0

我正在尝试将字符串发送到服务器,并且需要在特殊字符之前添加 \(例如:将“test '”替换为“test \'”)。我写 :

public static String handleSpecialChars(String str){
if (str!=null){
    Log.d("Common handleSpecialChars",str);
    str = str.replaceAll("[\\p{Punct}]", "\\$1");
   }
   return str;
}

但我得到了“test $1”(可能是因为 \ 指的是 $)。所以我也写了:

str = str.replaceAll("[\\p{Punct}]", "\\\\$1");

但比我得到“java.lang.ArrayIndexOutOfBoundsException”。我究竟做错了什么 ?

4

0 回答 0