【发布时间】:2011-02-25 07:40:07
【问题描述】:
在 Java 中寻找快速、简单的方法来更改此字符串
" hello there "
看起来像这样的东西
"hello there"
我用一个空格替换所有这些多个空格,除了我还希望字符串开头的一个或多个空格消失。
这样的事情让我部分地在那里
String mytext = " hello there ";
mytext = mytext.replaceAll("( )+", " ");
但不完全是。
【问题讨论】:
-
您应该考虑接受答案。它使稍后到达该页面的人更容易选择确定的解决方案。
-
这是最推荐的方式之一。 => 。字符串名称WithProperSpacing = StringUtils.normalizeSpace(stringWithLotOfSpaces);
-
s = s.replaceAll("\\s+"," ");