Grails Cookbook - A collection of tutorials and examples

Groovy Tokenize

We will show code snippet examples on how to tokenize a String in Groovy.

We can manipulate a String in Groovy to tokenize it, just simply invoke the tokenize() method of a String, for example:

def aString = "Hello World I am John Doe"
def tokens = aString.tokenize()
println "Number of tokens: " + tokens.size()
tokens.each { tok ->
	println tok
}

By default, if we do not specify the delimiter, white space are used. The output of the code above will be:

Number of tokens: 6
Hello
World
I
am
John
Doe

If the tokenize method can't find the delimiter, then only one item will be returned. here is an example:

def aString = "Part1:Part2:Part3"
def tokens = aString.tokenize()
println "Number of tokens: " + tokens.size()
tokens.each { tok ->
	println tok
}

And the output is just one item:

Number of tokens: 1
Part1:Part2:Part3

We can specify the delimiter to use when we tokenize a String in Groovy:

def aString = "Part1:Part2:Part3"
def tokens = aString.tokenize(":")
println "Number of tokens: " + tokens.size()
tokens.each { tok ->
	println tok
}

And the output is below:

Number of tokens: 3
Part1
Part2
Part3

We can specify more than one delimiter, for example:

def aString = "Num1+Num2-Num3"
def tokens = aString.tokenize("+-")
println "Number of tokens: " + tokens.size()
tokens.each { tok ->
	println tok
}

And below is the output:

Number of tokens: 3
Num1
Num2
Num3