Split string on white space

This example will show how to split a string by the whitespace delimiter in groovy. tokenize method is used in the first snippet while split is used in the second both resulting in the string broken apart on white space. Each method accepts a string and the only subtle difference is the split will return an Array of strings instead of a list. A similar example shows how to break apart a string by spaces in in java guava or apache commons.

Using Tokenize

@Test
void split_string_on_space_tokenize() {

    def baconSplit = "Bacon ipsum dolor sit amet eiusmod".tokenize(" ")

    assert ["Bacon", "ipsum", "dolor", "sit", "amet", "eiusmod"] == baconSplit
}

Using Split

@Test
void split_string_on_space_split() {

    def baconSplit = "Bacon ipsum dolor sit amet eiusmod".split(" ")

    assert ["Bacon", "ipsum", "dolor", "sit", "amet", "eiusmod"] == baconSplit
}