How to remove duplicate white spaces (including tabs, newlines, spaces, etc...) in a string using Java?
9 Answers
Like this:
yourString = yourString.replaceAll("\\s+", " ");
For example
System.out.println("lorem ipsum dolor \n sit.".replaceAll("\\s+", " "));
outputs
lorem ipsum dolor sit.
What does that \s+
mean?
\s+
is a regular expression. \s
matches a space, tab, new line, carriage return, form feed or vertical tab, and +
says "one or more of those". Thus the above code will collapse all "whitespace substrings" longer than one character, with a single space character.

- 413,195
- 112
- 811
- 826
-
3@SuhrobSamiev -- String.replaceAll() has been in Java since JDK 1.4. http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String) – David Moles Dec 22 '11 at 19:25
-
3I wish I could add more than +1 for the awesome explanation of \s+. – Cyntech Jun 01 '12 at 12:56
-
1I understood `\s+` but what does 2 backslash \\ mean ? – saplingPro Aug 23 '12 at 11:28
-
2The string literal `"\\"` represents the string consisting of a single backslash. So to represent `\s+` you write `"\\s+"`. – aioobe Aug 23 '12 at 11:51
-
so `\s` is a escape character and to start the escape character we start with \ . Is that so ? – saplingPro Aug 23 '12 at 12:40
-
No, `\s` is a regular expression that matches white-space (as stated in the answer). – aioobe Aug 23 '12 at 12:43
-
Nice solution, and well explained, but there is one pitfall: it does not trim the string. I know it's not explicitly asked in the description, but often when you want to remove duplicate whitespace from inside a string, you also want the string trimmed at start and end. – Wouter Lievens Feb 17 '13 at 21:39
-
From your answer I learned about Lorem ipsum:) It's a funny thing, try to type `=lorem(3,10)` in MS Word and press `Enter`! – naXa stands with Ukraine Jan 21 '14 at 18:08
-
1Will this remove carriage return? or would i have to remove "\\r" separately? Thanks! – user3388884 Sep 24 '14 at 20:28
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}"," ");

- 445,704
- 82
- 492
- 529
String str = " Text with multiple spaces ";
str = org.apache.commons.lang3.StringUtils.normalizeSpace(str);
// str = "Text with multiple spaces"

- 17,377
- 4
- 21
- 35
hi the fastest (but not prettiest way) i found is
while (cleantext.indexOf(" ") != -1)
cleantext = StringUtils.replace(cleantext, " ", " ");
this is running pretty fast on android in opposite to an regex

- 14,365
- 19
- 99
- 170
-
1Works only for spaces but not other whitespaces such as tabs and newlines. – Pang Aug 31 '13 at 06:41
-
1i know, you have to add more of these while loops for other entities. But this code run much faster on android as these regex, i had to process complete ebooks. – wutzebaer Sep 02 '13 at 09:22
-
Enormously faster on desktop too. Haven't tested it for a big string, but if you plan on running it on a lot of small strings this is the answer you are looking for. – Ivelate Feb 19 '18 at 02:31
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

- 54,530
- 11
- 89
- 103
Though it is too late, I have found a better solution (that works for me) that will replace all consecutive same type white spaces with one white space of its type. That is:
Hello!\n\n\nMy World
will be
Hello!\nMy World
Notice there are still leading and trailing white spaces. So my complete solution is:
str = str.trim().replaceAll("(\\s)+", "$1"));
Here, trim()
replaces all leading and trailing white space strings with "". (\\s)
is for capturing \\s
(that is white spaces such as ' ', '\n', '\t') in group #1. +
sign is for matching 1 or more preceding token. So (\\s)+
can be consecutive characters (1 or more) among any single white space characters (' ', '\n' or '\t'). $1
is for replacing the matching strings with the group #1 string (which only contains 1 white space character) of the matching type (that is the single white space character which has matched). The above solution will change like this:
Hello!\n\n\nMy World
will be
Hello!\nMy World
I have not found my above solution here so I have posted it.

- 1,126
- 4
- 16
- 32
If you want to get rid of all leading and trailing extraneous whitespace then you want to do something like this:
// \\A = Start of input boundary
// \\z = End of input boundary
string = string.replaceAll("\\A\\s+(.*?)\\s+\\z", "$1");
Then you can remove the duplicates using the other strategies listed here:
string = string.replaceAll("\\s+"," ");

- 1,067
- 1
- 14
- 20
You can also try using String Tokeniser, for any space, tab, newline, and all. A simple way is,
String s = "Your Text Here";
StringTokenizer st = new StringTokenizer( s, " " );
while(st.hasMoreTokens())
{
System.out.print(st.nextToken());
}

- 123
- 1
- 11
This can be possible in three steps:
- Convert the string in to character array (ToCharArray)
- Apply for loop on charater array
- Then apply string replace function (Replace ("sting you want to replace"," original string"));

- 202,337
- 40
- 393
- 406
-
1That's not a good solution, dropping to a char array doesn't solve anything. You're not actually explaining how to do the replace, which is the core of the problem. Also please **do not** post completely unrelated links. You'll get flagged as a spammer if you do so. – Mat Aug 21 '11 at 14:13