Java如何用字符串中的单个空格替换两个或多个空格,并删除前导和尾随空格在Java中寻找快速、简单的方法来更改此字符串" hello there "像这样的东西"hello there"其中,我用一个空格替换所有这些多个空格,但我也希望字符串开头的一个或多个空格消失。像这样的东西让我在一定程度上String mytext = " hello there ";mytext = mytext.replaceAll("( )+", " ");但不完全是。
3 回答

沧海一幻觉
TA贡献1824条经验 获得超5个赞
replaceAll("\\s{2,}", " ").trim();
System.out.println(new String(" hello there ").trim().replaceAll("\\s{2,}", " "));
"hello there"
添加回答
举报
0/150
提交
取消