10

How to write a regex to remove spaces in Java?

For example

Input  : "         "
Output : ""
---------------------
Input  : " "
Output : ""

Note that tabs and new lines should not be removed. Only spaces should be removed.

Edit :

How do we make a check ?

For example how to check in an if statement that a String contains only spaces (any number of them)

  if(<statement>)
  {
              //inside statement
  }

For

   input = "                   "  or input = "    "

The control should should go inside if statement.

1
  • I've reworded your question a bit because "whitespace" includes tabs and newlines (and formfeeds etc.), so you should use the term "space" instead. Commented May 15, 2012 at 7:56

4 Answers 4

12

The following will do it:

str = str.replaceAll(" ", "");

Alternatively:

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

In my benchmarks, the latter was ~40% faster than the former.

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

1 Comment

@ChristopheD: Oh yes it is :-) The " " is a regex that matches one space.
6
String str = "     ";
str = str.replaceAll("\\s+","");

Comments

1

you can do -

str = str.replace(" ", "");
str = str.replaceAll(" +", "");

If you check definition of replaceandreplaceAll method -

public String replace(CharSequence target, CharSequence replacement) {
        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
            this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
    }

public String replaceAll(String regex, String replacement) {
   return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

Unless you really need to replace a regular expression, replaceAll is definitely not our choice. Even if the performance is acceptable, the amount of objects created will impact the performance.

Not that much different from the replaceAll(), except that the compilation time of the regular expression (and most likely the execution time of the matcher) will be a bit shorter when in the case of replaceAll(). Its better to use replace method rather replaceAll.

Real-time comparison

File size -> 6458400
replaceAll -> 94 millisecond
replace -> 78 millisecond

2 Comments

Can you time and compare str.replace(" ", ""); versus str.replaceAll(" +", "");? That would be interesting.
@TimPietzcker - kindly check the test result.
0

Why not try a simple /[ ]+/? This should be what you need.

4 Comments

Wont \s capture tabs also? Question doesn't want the tabs to be removed.
yeah that was dumb of me. Using a character class is probably more appropriate here.
You should at least use + as a quantifier (otherwise you'll do a "substitute nothing with nothing" operation on every location between two non-space characters in the entire string). Also, drop the brackets - it's only one character, so you don't need a character class (although one might argue it helps readability, and Java's regex engine will hopefully be smart enough to optimize it away).
Thank you very much for your suggestions. I will leave the brackets because I like the flexibility and (as you mentioned) imporved readability. But I will add the plus though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.