由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - why doesn't replaceAll work?
相关主题
java 的内存分布?问个小问题
有没有 replaceall for String?Java大侠们:Hashtable help please!
问个String:replaceALL的问题expression in unicode
有什么办法高效的进行replaceAllTIJ上写错了?
java 截取一部分string一道 JAVA Stack vs Heap 题
我自己编了个Java面试题问一个Java题
如何确保每次读入的字符串都是unique的Java练习题 3
出个简单题,看你Java APi熟悉到什么程度string pool vs class constant pool问题,在线等
相关话题的讨论汇总
话题: string话题: replaceall话题: foo话题: doesn
进入Java版参与讨论
1 (共1页)
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

1 (共1页)
进入Java版参与讨论
相关主题
string pool vs class constant pool问题,在线等java 截取一部分string
问个set和literal String的问题我自己编了个Java面试题
String[] a = c.toArray(new String[0])如何确保每次读入的字符串都是unique的
about builder pattern出个简单题,看你Java APi熟悉到什么程度
java 的内存分布?问个小问题
有没有 replaceall for String?Java大侠们:Hashtable help please!
问个String:replaceALL的问题expression in unicode
有什么办法高效的进行replaceAllTIJ上写错了?
相关话题的讨论汇总
话题: string话题: replaceall话题: foo话题: doesn