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
  • Where are you stuck? Commented Sep 25, 2014 at 14:02
  • This was answered here Commented Sep 25, 2014 at 14:03

2 Answers 2

3

With regex try

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

thats preatty it.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not correct. It should be "my extra spaced string".replaceAll("\\s+"," ")
1

Use this:

   text = text.replaceAll("\\s{2,}", " ");

2 Comments

and how to replace multiple space in text file line by line?
you might want to look at stackoverflow.com/questions/20039980/… and then use the answer from this question to do the replacing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.