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.
Straight up Java
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.
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
This snippet will split text into an array using whitespace as a separator or delimiter using apache commons StringUtils.Split.