10/31/2010

Split a string in Java

If you have a string as follows:

Lorem Ipsum   Foo Bar

and you want to split it by whitespaces, you can't do

myString.split(" ");

because the output would be ("Lorem", "Ipsum", " ", "Foo", "Bar")

Obviously the desired output would be: ("Lorem", "Ipsum", "Foo", "Bar")

To do that in java use: myString.split("\\s+");