Reputation: 1554
Using Java, how can I replace all consecutive white spaces with a single space, in an elegant and simple way?
I am aware of str.replaceAll("\\s+", " ");
but this replaces \n
and other things too. I only want to replace multiple one after another with a single
.
Upvotes: 0
Views: 220
Reputation: 124235
Most regex flavors considers \n
as whitespace. Same about \r
or \t
.
If you want to replace only one or more " "
the simply use replaceAll(" +", " ")
Upvotes: 2