This example will show how to split a string on a whitespace using java, guava and apache commons. You could easily replace what character you want to split on like a dot, backslash, escape or any special character. In the initial set up we will create a String which each technique will use to split.
Setup
Straight up Java
Stringtokenizer
This snippet will show how to split a string into separate strings using Stringtokenizer. A Stringtokenizer will construct a tokenizer of a specified string based on the default delimiter set. Then we will use a while loop to loop over each token displaying it in the console.
Output
String split
Using a regular expression we will show how to use the String.split function to break on white spaces.
We will split a string on a whitespace using guava's Splitter class. To construct a splitter we will specify which character it should use as separator to break apart the string. Next we will call the split function that will split the sequence into chunks and make them available via an Iterator.
Split on whitespace
Output
Apache Commons
This snippet will split text into an array using whitespace as a separator or delimiter using apache commons StringUtils.Split.