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.
精彩评论