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
Java has inbuilt methods to remove the whitespaces from the string, whether leading, trailing, both, or all. trim() method removes the leading and trailing spaces present in the string. strip() method removes the leading and trailing spaces present in the string.
You can also remove spaces using the Find/Replace feature in Excel. Click CTRL+F to open the Find dialog box, then click the Replace tab. Enter one space ” ” in the Find what: field and leave the Replace with: field empty to remove all spaces.
Use \\s+
instead of \\s
as there are two or more consecutive whitespaces in your input.
string = str.replaceAll("\\s+"," ")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With