开发者

how can i reduce the number of whitespaces in a sentence to 1 if there are more then 1 whitespaces in the sentence?

How can i r开发者_如何学Goeduce the number of whitespaces in a sentence to 1 if there are more then 1 whitespaces in the sentence?


Assuming you mean "consecutive" whitespaces, here's a solution.

String sentence = "hello   here are \n some   whitespaces.";
String newString = sentence.replaceAll("\\s+", " ");
System.out.println(newString);

Output:

hello here are some whitespaces.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜