How to remove duplicate white spaces(including tabs,newlines,spaces,etc...) in a string using Java?
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
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.
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}"," ");
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]);
}