We're All Mad Here
We're All Mad Here

Reputation: 1554

Replace consecutive white spaces with a single space

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

Answers (1)

Pshemo
Pshemo

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

Related Questions