Java split() of String | Multiple whitespace characters

The split method of the String class is very useful when you want to tokenize a string. Its power lies in the fact that it accepts a string, as a parameter, which can be a regular expression. However you must be careful when you want to split a string using the whitespace character as a delimiter. Consider the following snippet of code:

String str = "Testing split using two  whitespace characters";
String[] tokens = str.split("\\s");
for(String token : tokens) {
  System.out.println("-" + token + "-");
}

What’s the output produced by the previous code? If you think it is the following one you’re wrong:

-Testing-
-split-
-using-
-two-
-whitespace-
-characters-

The actual output is instead this one:


-Testing-
-split-
-using-
-two-

-whitespace-
-characters-

Where in the hell did that empty string come out from? It comes out from the two whitespace characters that are between the word two and whitespace of the str string. If this is what you want OK. However, most of the time, you will want to discard that empty string from your resulting string array. You can obtain this result by using the \\s+ regex in place of \\s. Basically, the previuos code becomes:

String str = "Testing split using two  whitespace characters";
String[] tokens = str.split("\\s+");
for(String token : tokens) {
  System.out.println("-" + token + "-");
}

This entry was posted in IT, Programming and tagged , . Bookmark the permalink.

3 Responses to Java split() of String | Multiple whitespace characters

  1. Rob says:

    Is there a way to capture (Define) as a value one of the words in the string?

  2. I’m not sure I understood your question, but the split method splits the string into a string array so you can point to the word you need by using common array accessing methods.

  3. Julia says:

    Sure hun use tokens[0], tokens[1] etc for the first second words ….. Its in an array after all :)

    Julia XX

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>