Rishi Rdy
Rishi Rdy

Reputation: 215

I would like to Trim Spaces which is in a single string

                       :     VJ NT jdbc & hyC rayan asdALAYA
                                                                8 CHURCH ROAD 9 TH FLOOR
                                                PUNE 3256-34546561
                                                PUNE 412346

this is in a single string which has lots of spaces I would like trim all extra spaces and give a single space for each word.Please help me

Upvotes: 0

Views: 37

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

You could try replacing all blocks of multiple spaces with a single space:

String input = "    :     VJ NT jdbc & hyC rayan asdALAYA";
input = input.replaceAll("\\s+", " ");

If you also want to remove a leading or trailing space which might be leftover, you can use String.trim() for that:

input = input.trim();

Upvotes: 3

Related Questions