views:

316

answers:

4

How to remove duplicate white spaces(including tabs,newlines,spaces,etc...) in a string using Java?

+2  A: 

Try this - You have to import java.util.regex.*;

    Pattern pattern = Pattern.compile("\\s+");
    Matcher matcher = pattern.matcher(string);
    boolean check = matcher.find();
    String str = matcher.replaceAll(" ");

Where string is your string on which you need to remove duplicate white spaces

Sachin Shanbhag
+20  A: 

Like this:

yourString = yourString.replaceAll("\\s+", " ");

For example

System.out.println("lorem  ipsum   dolor \n sit.".replaceAll("\\s+", " "));

outputs

lorem ipsum dolor sit.

Detailed explanation: \s matches a space, tab, new line, carriage return, form feed and vertical tab and + says "one or more times". Thus it will collapse all "whitespace substrings" longer than one character, with a single space character.

aioobe
+3  A: 

You can use the regex

(\s)\1

and

replace it with $1.

Java code:

str = str.replaceAll("(\\s)\\1","$1");

If the input is "foo\t\tbar " you'll get "foo\tbar " as output
But if the input is "foo\t bar" it will remain unchanged because it does not have any consecutive whitespace characters.

If you treat all the whitespace characters(space, vertical tab, horizontal tab, carriage return, form feed, new line) as space then you can use the following regex to replace any number of consecutive white space with a single space:

str = str.replaceAll("\\s+"," ");

But if you want to replace two consecutive white space with a single space you should do:

str = str.replaceAll("\\s{2}"," ");
codaddict
A: 

Good old fashioned way (non regex :))

String input = "    Hello world    \t\t";

String[] whitespaces = { "  ", "\t\t", "\n\n" };
String[] replacements = { " ", "\t", "\n\n" };

for (int i = 0; i < whitespaces.length; i++)
{
      input = input.replace(whitespaces[i], replacements[i]);
}
Richard J. Ross III
Try it out... it doesn't work.
aioobe
worked on my test...
Richard J. Ross III
Hmm.. strange. Care to put your test on ideone.com or some other pastebin service? I'd like to see where I did wrong.. because I can't explain why it doesn't work for me :P
aioobe
when I run it, it gives the expected output of ' Hello world' on the test project of http://ideone.com/9pOTG.. run it and tell me if I am wrong...
Richard J. Ross III
Ah, well, some duplicates may still be in the input string after processing: http://ideone.com/EJjYi (See the initial two space for instance.)
aioobe