我正在尝试将字符串发送到服务器,并且需要在特殊字符之前添加\(例如:将“test”替换为“test \”“)。我写道:
public static String handleSpecialChars(String str){
if (str!=null){
Log.d("Common handleSpecialChars",str);
str = str.replaceAll("[\\p{Punct}]", "\\$1");
}
return str;
}
但我得到了“测试$ 1”(可能是因为\指的是$)。所以我也写道:
str = str.replaceAll("[\\p{Punct}]", "\\\\$1");
但是我得到了“java.lang.ArrayIndexOutOfBoundsException”。 我做错了什么?