Lav patel Lav patel - 1 year ago 92
Java Question

How to remove spaces in between the String

I have below String

string = "Book Your Domain And Get\n \n\n \n \n \n Online Today."
string = str.replace("\\s","").trim();

which returning

str = "Book Your Domain And Get Online Today."

But what is want is

str = "Book Your Domain And Get Online Today."

I have tried Many Regular Expression and also googled but got no luck. and did't find related question, Please Help, Many Thanks in Advance

Answer Source

Use \\s+ instead of \\s as there are two or more consecutive whitespaces in your input.

string = str.replaceAll("\\s+"," ")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download