我有一个叫做的字符串 persons.name
我想.用/*/ 我的输出替换DOTpersons/*/name
我试过这段代码:
String a="\\*\\";
str=xpath.replaceAll("\\.", a);
Run Code Online (Sandbox Code Playgroud)
我收到了StringIndexOutOfBoundsException.
如何更换圆点?
Fem*_*emi 136
在点之前需要两个反斜杠,一个用于逃避斜线以使其通过,另一个用于逃避点以使其变为文字.正斜杠和星号按字面处理.
str=xpath.replaceAll("\\.", "/*/"); //replaces a literal . with /*/
Run Code Online (Sandbox Code Playgroud)
khe*_*ood 12
如果你想替换一个简单的字符串并且你不需要正则表达式的能力,你可以使用replace,而不是replaceAll。
replace 替换每个匹配的子字符串,但不将其参数解释为正则表达式。
str = xpath.replace(".", "/*/");
Run Code Online (Sandbox Code Playgroud)
String a= "\\*\\";
str = StringUtils.replace(xpath, ".", a);
Run Code Online (Sandbox Code Playgroud)
或者使用独立的JDK:
String a = "\\*\\"; // or: String a = "/*/";
String replacement = Matcher.quoteReplacement(a);
String searchString = Pattern.quote(".");
String str = xpath.replaceAll(searchString, replacement);
Run Code Online (Sandbox Code Playgroud)