Split string

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

private static final String CONSTANT_STRING = "The Dog goes woof, The cat goes meow, the cow says moo, the duck says quack";

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.

@Test
public void split_string_using_stringtokenizer_java () {

    StringTokenizer stringTokenizer = new StringTokenizer(CONSTANT_STRING);

    int numberOfTokens = stringTokenizer.countTokens();

    while (stringTokenizer.hasMoreElements()) {
        logger.info(stringTokenizer.nextElement());
    }

    assertTrue(numberOfTokens == 16);
}

Output

The
Dog
goes
woof,
The
cat
goes
meow,
the
cow
says
moo,
the
duck
says
quack

String split

Using a regular expression we will show how to use the String.split function to break on white spaces.

@Test
public void split_string_using_string_split_java () {
    String delims = "[ ]+";
    String[] tokens = CONSTANT_STRING.split(delims);

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

    assertTrue(tokens.length == 16);
}

Output

[The, Dog, goes, woof,, The, cat, goes, meow,, the, cow, says, moo,, the, duck, says, quack]

Java 8

First generating 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. Finally converting the stream into list will allow an assertion to verify the split was successful. Alternatively if you wanted to print the elements you could by using Stream.foreach.

@Test
public void split_string_using_string_split_java8() {

    List<String> splitString = Stream.of(CONSTANT_STRING)
            .map(w -> w.split("[ ]+")).flatMap(Arrays::stream)
            .collect(Collectors.toList());

    logger.info(splitString);

    assertTrue(splitString.size() == 16);
}

Output

[The, Dog, goes, woof,, The, cat, goes, meow,, the, cow, says, moo,, the, duck, says, quack]

Google Guava

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

@Test
public void split_string_whitespace_using_guava() {

    List<String> elementsInString = Lists.newArrayList(Splitter.on(" ")
            .split(CONSTANT_STRING));

    logger.info(elementsInString);

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

Output

[The, Dog, goes, woof,, The, cat, goes, meow,, the, cow, says, moo,, the, duck, says, quack]

Apache Commons

This snippet will split text into an array using whitespace as a separator or delimiter using apache commons StringUtils.Split.

Split on whitespace

@Test
public void split_string_white_space_using_apache_commons () {

    String[] elementsInString = StringUtils.split(CONSTANT_STRING);

    logger.info(elementsInString);

    assertTrue(elementsInString.length == 16);
}

Output

[The, Dog, goes, woof,, The, cat, goes, meow,, the, cow, says, moo,, the, duck, says, quack]