Split string on whitespace

This example will show how to split a string by the whitespace delimiter in java, guava and apache commons. We will use the opening line from the song "Let it go", The snow glows white on the mountain tonight. Each element will be broken on a space which results in 8 elements. You could replace the space for any punctuation and get expected results. A similar example shows how to break apart a string with spaces in groovy.

Split using StringTokenizer

public void split_on_whitespace_stringtokenizer() {

    StringTokenizer stringTokenizer = new StringTokenizer(
            "The snow glows white on the mountain tonight");

    int numberOfTokens = stringTokenizer.countTokens();

    while (stringTokenizer.hasMoreElements()) {

    assertTrue(numberOfTokens == 8);

Split using regex

public void split_on_whitespace_split_regex() {
    String[] tokens = "The snow glows white on the mountain tonight"


    assertTrue(tokens.length == 8);

Java 8

Creating a stream from a string then applying the java function to the elements will produce an Arraylist of String arrays. Calling the flatmap will combine each array into a single stream by flattening it. Finally we will convert stream into list.

public void split_string_whitespace_java8() {

    List<String> splitOnWhitespace = Stream
            .of("The snow glows white on the mountain tonight")
            .map(w -> w.split("\\s+")).flatMap(Arrays::stream)


    assertTrue(splitOnWhitespace.size() == 8);

Google Guava

public void split_string_whitespace_using_guava() {

    List<String> elementsInString = Lists.newArrayList(Splitter.on(" ")
            .split("The snow glows white on the mountain tonight"));


    assertTrue(elementsInString.size() == 8);

Apache Commons

public void split_string_white_space_using_apache_commons() {

    String[] elementsInString = StringUtils
            .split("The snow glows white on the mountain tonight");


    assertTrue(elementsInString.length == 8);