April 23, 2010

Groovy Goodness: Tokenize a String

Groovy adds the tokenize() method to the String class. We can use this method to split up a string value using a String, char or whitespace as delimiter. Groovy also adds a split() method without arguments that uses whitespace as delimiter, but returns a String array instead of List.

def s = 'one two three four'

def resultList = s.tokenize()
assert resultList.class.name == 'java.util.ArrayList'
assert ['one', 'two', 'three', 'four'] == resultList

def resultArray = s.split()
assert resultArray instanceof String[]
assert ['one', 'two', 'three', 'four'] == resultArray

def s1 = 'Java:Groovy'
assert ['Java', 'Groovy'] == s1.tokenize(":")
assert ['Java', 'Groovy'] == s1.tokenize(':' as char)