String.replaceAll奇怪的行为
String s = "hi hello"; s = s.replaceAll("\\s*", " "); System.out.println(s);
我有上面的代码,但我无法弄清楚它为什么产生
hihello
而不是
hi hello
非常感谢
使用+
量词匹配1个或多个空格而不是*
: –
s = s.replaceAll("\\s+", " ");
\\s*
表示匹配0个或更多空格,并在每个字符前匹配一个空字符,并用空格替换。
*
匹配0或更多空格,我认为您要将其更改为+
以匹配1个或更多空格。