【发布时间】:2013-08-22 00:31:08
【问题描述】:
String s = "hi hello";
s = s.replaceAll("\\s*", " ");
System.out.println(s);
我有上面的代码,但我不知道它为什么会产生
h i h e l l o
而不是
hi hello
非常感谢
【问题讨论】:
String s = "hi hello";
s = s.replaceAll("\\s*", " ");
System.out.println(s);
我有上面的代码,但我不知道它为什么会产生
h i h e l l o
而不是
hi hello
非常感谢
【问题讨论】:
使用+ 量词来匹配1 个或多个空格而不是*:-
s = s.replaceAll("\\s+", " ");
\\s* 表示匹配0个或多个空格,并且会在每个字符前匹配一个空字符并替换为空格。
【讨论】:
* 匹配 0 个或多个空格,我想您想将其更改为 + 以匹配 1 个或多个空格。
【讨论】: