How to remove Whitespace between String – Java

Today i play around with some funny and simple thing in Java, frankly i really do not know about it. For example, see code snippets :


if(StringUtils.hasLength(sText)
 {
     sText= sText.replaceAll(" ", " AND ");			   
}

What i trying to do is remove all whitespace between a string. for example, user key in “Hello Google”, it will replace as “Hello AND Google”, ya this is correct if user only enter one space between words.

"Hello Google"  ---> "Hello AND Google"

But how about user key in many spaces in between? For example

"Hello           Google"  ---> "Hello AND AND AND AND AND Google"

Above is not what i want, should i replace it as below?


if(StringUtils.hasLength(sText)
 {
     sText= sText.replaceAll(" ", " AND ");
     sText= sText.replaceAll("  ", " AND ");	
     sText= sText.replaceAll("   ", " AND ");	
     sText= sText.replaceAll("    ", " AND ");	
     sText= sText.replaceAll("     ", " AND ");				   
}

ya we can replace as many as we like, but this is not so effective and dynamic enough.

Solution

In this case, you should use regular expression to solve it.


if(StringUtils.hasLength(sText)
 {
     sText= sText.replaceAll("\\s+", " AND ");		   
}

Great , exactly what i want, Regular Expression ROCK!!!

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Edwin Paul C
Guest
Edwin Paul C

String mysz = ” Hello Java “;
System.out.println(“[“+mysz.trim()+”]”); //Removes space from left and right –> [Hello Java]
System.out.println(“[“+mysz.replaceAll(“\W+”, ” “)+”]”); //Removes space from middle –> [ Hello Java ]
System.out.println(“[“+mysz.replaceAll(“\W+”, ” “).trim()+”]”); ////Removes space from middle, left and right–> [Hello Java]

komal momin
Guest
komal momin

Thank you so very much. I always find exactly what I need on your website!

G.Lee
Guest
G.Lee

Thank you so much. I’ve found many answers to solve errors in my codes on this website.

Binoy Kumar Baranwal
Guest
Binoy Kumar Baranwal

Yes mkYong. You are right.Regular Expression(regex) is rock. I am already working on regex from last 1 year. I got your updates in my mail. That is why I came back here once again.

Thanks.

Dzik
Guest
Dzik

how remove examples

String exam=” Java CSS HTML\n\n Good”

output =”Java CSS HTML
Good”

Binoy Kumar Baranwal
Guest
Binoy Kumar Baranwal

hello Dzik, This is the answer for your question.

Thanks.

public class HelloWorld{

public static void main(String []args){

String exam=” Java CSS HTML\n\n Good”;
System.out.println(“Input=”+exam);
exam = exam.replaceAll(“^\\s+”,””);
exam = exam.replaceAll(“\n\n\\s+”,”\n”);
System.out.println(“———————–“);
System.out.println(“Output=”+exam);

}
}

Raj
Guest
Raj
karthik
Guest
karthik

public class RemoveSpace {

public static void main(String[] args) {
// TODO Auto-generated method stub
String s=”hello hai this is karthik”;
System.out.println( s.replaceAll(“\\s+”, ” “));

}

}

//input:hello hai this is karthik
//output:hello hai this is karthik

Tushar Sharma
Guest
Tushar Sharma

Thanks a lot, this code helped me a lot :) ….

rajeev
Guest
rajeev

but if i want to replace all multiple whitespace character not single then

BINOY KUMAR BARANWAL
Guest
BINOY KUMAR BARANWAL

Yes… Regular expression is really rock.

António Santos
Guest
António Santos

This is it.
Thank you

Amol
Guest
Amol

Found here the solution.Exactly what I was searching the internet for …
Thanks

Anuradha
Guest
Anuradha

I have a combobox value like abc_45;I need only the id such as 45 to store into the database while selected.

please help…..

Someone
Guest
Someone
Although your conclusion regarding the regex pattern is correct, the function you “suggest” as a bad example is actually worse than just bad practice. It simply won’t work. if(StringUtils.hasLength(sText) { sText= sText.replaceAll(" ", " AND "); sText= sText.replaceAll(" ", " AND "); sText= sText.replaceAll(" ", " AND "); sText= sText.replaceAll(" ", " AND "); sText= sText.replaceAll(" ", " AND "); } The first replaceAll used on "Hello Google" will result in "Hello AND AND AND AND AND AND AND AND AND AND AND AND Google" and the subsequent replaceAlls will never be matching anything.
anil
Guest
anil

i got it boss… Excellent job keep it up……….

anil
Guest
anil

which libray file i have to import for this function…. mail me ..help please

vidhya
Guest
vidhya

Nice one.. u rock!!

Emmanuel
Guest
Emmanuel

Thanks for take your time to post such a usefull tips, this is what I was looking for.

Thanks again,
Emmanuel

Frank
Guest
Frank

Excellent!

Frederic
Guest
Frederic

Thank you!!
Exactly what I was looking for!

Cheers