0

What i want to do is to replace multiple space to single space in a string.

I have string variable

String text = "i  want  to  replace   multiple space";

and i want it change into

String text = "i want to replace multiple space";

how can i do it?, help me please, thanks before.

2 Answers2

3

With regex try

"my   extra spaced   string".replaceAll("\\s+?"," "); 

thats preatty it.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
1

Use this:

   text = text.replaceAll("\\s{2,}", " ");
jarz
  • 732
  • 7
  • 20