Split string on comma

This example will show how to split a string by using a comma 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 a comma. 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 split comma delimited string in in java guava or apache commons.

Using Tokenize

void split_string_on_comma_tokenize() {

    def seperateStringByComma = "and, but, for, nor, yet, or, so".tokenize(", ")

    assert ["and", "but", "for", "nor", "yet", "or", "so"] == seperateStringByComma

Using Split

void split_string_on_comma_split() {

    def splitStringComma = "and, but, for, nor, yet, or, so".split(", ")

    assert ["and", "but", "for", "nor", "yet", "or", "so"] == splitStringComma