如下:
String s = "工作\r";
System.out.println("s = "+s);
System.out.println("s1 = "+s.replaceAll("\r", "r"));
System.out.println("s2 = "+s.replaceAll("\r", "\r"));
System.out.println("s3 = "+s.replaceAll("\r", "\\r"));
打印结果:
s = 工作
s1 = 工作r
s2 = 工作
s3 = 工作r
我想了半天也想不明白为什么s3打印出来是:工作r
我觉得应该打出“工作”然后换行。
知道的兄弟给我仔细讲一讲呗。
------解决方案--------------------
跟踪下源码,或许能理解:
String 类的replaceAll方法是:
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
可见是调用了Matcher里的replaceAll方法。
public String replaceAll(String replacement) {
reset();
boolean result = find();
if (result) {
StringBuffer sb = new StringBuffer();
do {
appendReplacement(sb, replacement);//看下面这个方法
result = find();
} while (result);
appendTail(sb);
return sb.toString();
}
return text.toString();
}
...
...
...
public Matcher appendReplacement(StringBuffer sb, String replacement) {
// If no match, return error
if (first < 0)
throw new IllegalStateException("No match available");
// Process substitution string to replace group references with groups
int cursor = 0;
String s = replacement;
StringBuffer result = new StringBuffer();
while (cursor < replacement.length()) {
char nextChar = replacement.charAt(cursor);
if (nextChar == '\\') {//这块对'\'的处理是第一次遇到这个'\',跳过了。直接取后边的字符。
cursor++;
nextChar = replacement.charAt(cursor);
result.append(nextChar);
cursor++;
} else if (nextChar == '$') {
// Skip past $
cursor++;
// The first number is always a group
int refNum = (int)replacement.charAt(cursor) - '0';
if ((refNum < 0)
------解决方案--------------------
(refNum > 9))
throw new IllegalArgumentException(
"Illegal group reference");
cursor++;
// Capture the largest legal group string
boolean done = false;