这条线
System.out.println("\\");
打印一个反斜杠符号 (\
)。
System.out.println("\\\\");
打印双反斜杠 (\\
)。明白了!
但在下面的代码中为什么要这样写:
class ReplaceTest
{
public static void main(String[] args)
{
String s = "hello.world";
s = s.replaceAll("\\.", "\\\\");
System.out.println(s);
}
}
输出是否为:
hello\world
取代
hello\\world
毕竟,replaceAll()
方法是将一个点(\\.
)替换为(\\\\
)。
有人可以解释一下吗?
\\.
解析为\.
,然后正则表达式会将其解析为.
? - ametren