在 Java 中将单个“\”替换为“\\”
2022-09-04 05:22:33
如何将单曲替换为 ?当我运行时,我收到此错误消息。'\'
'\\'
replaceAll()
Exception in thread "main" java.util.regex.PatternSyntaxException:
Unexpected internal error near index 1 \
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.compile(Pattern.java:1466)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
at java.lang.String.replaceAll(String.java:2190)
at NewClass.main(NewClass.java:13)
Java Result: 1
我的代码:
public class NewClass {
public static void main(String[] args) {
String str = "C:\\Documents and Settings\\HUSAIN\\My Documents\\My Palettes";
str = str.replaceAll("\\", "\\\\");
System.out.println(str);
}
}