m****r 发帖数: 6639 | 1 String foo = '\' + '\';
foo.replaceAll("\\\\", "\\");
would cause an indexOutOfBounds exception.... i don't understand. | Q**g 发帖数: 183 | 2 看API啊。
我猜你是想要把两个backslashes换成一个是吧?应该这么写:
String foo = "\\" + "\\";
foo.replaceAll("\\\\\\\\", "\\\\");
replaceAll的第一个参数是regular expression。regular expression里
要用两个backslash '\\' 表示单个字符 '\'。然后因为你在java里头写字符串
常量也是'\\'表示一个'\'。所以第一个参数需要一共8个'\'。
第二个参数里头'\'也是有特殊含义的,所以也是。。。。
Note that backslashes (\) and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement string. Dollar signs may be
treated as references to captured subs
【在 m****r 的大作中提到】 : String foo = '\' + '\'; : foo.replaceAll("\\\\", "\\"); : would cause an indexOutOfBounds exception.... i don't understand.
| A**o 发帖数: 1550 | 3
'\' + '\' --> '\' + "" + '\'
【在 m****r 的大作中提到】 : String foo = '\' + '\'; : foo.replaceAll("\\\\", "\\"); : would cause an indexOutOfBounds exception.... i don't understand.
| m****r 发帖数: 6639 | 4 i got the first "\\\\\\\\", but thought the second part was just a normal
java string. anyway, thx, it works now.
【在 Q**g 的大作中提到】 : 看API啊。 : 我猜你是想要把两个backslashes换成一个是吧?应该这么写: : String foo = "\\" + "\\"; : foo.replaceAll("\\\\\\\\", "\\\\"); : replaceAll的第一个参数是regular expression。regular expression里 : 要用两个backslash '\\' 表示单个字符 '\'。然后因为你在java里头写字符串 : 常量也是'\\'表示一个'\'。所以第一个参数需要一共8个'\'。 : 第二个参数里头'\'也是有特殊含义的,所以也是。。。。 : Note that backslashes (\) and dollar signs ($) in the replacement : string may cause the results to be different than if it were being
|
|