0

My requirement is remove all unnecessary spaces from a string with using regular expression.

I have a string like this:

"name=john    age=26     year=1999";  

I want to remove Unnecessary space between two eliminator, expecting output

"name=john age=26 year=1999;"

4 Answers4

4
account = account.replaceAll("\\s+", " ");
Jainendra
  • 24,713
  • 30
  • 122
  • 169
0

You can use String#replaceAll and use reg-ex "[ \t]+" to replace multiple space with a single one like this:

 account.replaceAll("[ \t]+", " ");

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0

What about this?

    String before ="    name=john   age=26     year=1999  ";  
    String  after = before.replaceAll("\t", " ");
    after = after.trim().replaceAll(" +", " ");
    System.out.println(after);
Nidheesh
  • 4,390
  • 29
  • 87
  • 150
-3

There's a regular expressions for finding space, now just replace it

/\s+/
waplet
  • 151
  • 1
  • 9