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
  • 23,989
  • 30
  • 120
  • 167
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,798
  • 2
  • 20
  • 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,192
  • 27
  • 78
  • 144
-3

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

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