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

@Test
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()) {
        logger.info(stringTokenizer.nextElement());
    }

    assertTrue(numberOfTokens == 8);
}

Split using regex

@Test
public void split_on_whitespace_split_regex() {
    String[] tokens = "The snow glows white on the mountain tonight"
            .split("\s+");

    logger.info(Arrays.toString(tokens));

    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.

@Test
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)
            .collect(Collectors.toList());

    logger.info(splitOnWhitespace);

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

Google Guava

@Test
public void split_string_whitespace_using_guava() {

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

    logger.info(elementsInString);

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

Apache Commons

@Test
public void split_string_white_space_using_apache_commons() {

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

    logger.info(Arrays.toString(elementsInString));

    assertTrue(elementsInString.length == 8);
}