如何解决Java replaceAll字符串的功能不会替换[duplicate]
| 这个问题已经在这里有了答案:解决方法
您不需要正则表达式,因此请使用:
test.replace(\"()\",\"\")
,正如其他人指出的那样,由于不需要正则表达式,因此在这种情况下您可能要使用String.replace
。
作为参考,使用String.replaceAll
时,第一个参数(被解释为正则表达式)需要加引号,最好使用Pattern.quote
:
String test = \"replace()thisquotes\";
test = test.replaceAll(Pattern.quote(\"()\"),\"\");
// ^^^^^^^^^^^^^
System.out.println(test); // prints \"replacethisquotes\"
,replaceAll函数的第一个参数是一个正则表达式。 \“(\”字符是正则表达式中的特殊字符。
用这个 :
public class Main {
public static void main(String[] args) {
String test = \"replace()thisquotes\";
test = test.replaceAll(\"\\\\(\\\\)\",\"\");
System.out.println(test);
}
}
,您必须转义ѭ6,因为这些是常规字符保留的字符:
String test = \"replace()thisquotes\";
test = test.replaceAll(\"\\\\(\\\\)\",\"\");
,test = test.replaceAll(\"\\\\(\\\\)\",\"\").
Java替换了所有使用的正则表达式,因此在您的示例中\“()\”是一个空组,请使用转义字符\'\\\“。
,您必须先引用String,因为括号是正则表达式中的特殊字符。看一下Pattern.qutoe(String s)
。
test = test.replaceAll(Pattern.quote(\"()\"),\"\");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。