Grails Cookbook - A collection of tutorials and examples

Groovy Split Examples

Groovy String has a split() method as a convenience method to split a String or character sequence and return the result as Array of String. The given String or character sequence is split by whitespace as a default, or the given delimiter. Below are some examples on how to use Groovy String's split() method.

Groovy Split()

When no parameter is passed, it is assumed that the String will be broken using whitespace as delimiter. Example:
def sampleText = "Groovy is Cool"
println sampleText.split()

The code will break the String into the words separated by whitespace. Hence we get three items. This will print:

[Groovy, is, Cool]

Whitespace does not only mean space. It includes tabs and next line as delimiter. The code below will yield the same output:

def sampleText = "Groovy is\nCool"
println sampleText.split()

Having extra whitespaces will not result into empty String as part of the result.
def sampleText = "Groovy  is   Cool"
println sampleText.split()

Will only have 3 items:

[Groovy, is, Cool]

Groovy Split() Result Type

Note that the result of split() is an Array and not List. Consider the code:
def sampleText = "Groovy is\nCool"
println sampleText.split() instanceof String[]
println sampleText.split() instanceof List

Will return true for the first line because the result is an array of String, and false for the second because it is not an instance of a List:


Groovy Split() with Delimiter

We can supply specific delimiter to Groovy String's split() method. For example:
def sampleText = "AxBxC"
println sampleText.split('x')

The code will print:

[A, B, C]

It will disregard whitespace, example:

def sampleText = "AxBxC E F"
println sampleText.split('x')

Will not use space as delimiter, but the x only. Hence, we only get three items:

[A, B, C E F]

If the delimiter is more than one character, it treats it as a whole delimiter, and not as each character as individual delimiter. For example:

def sampleText = "AxBxCxyEyFyG"
println sampleText.split('xy')
Will not treat x and y separately as delimiters, but xy as a whole. Hence the output will be:
[AxBxC, EyFyG]

If we specify a delimiter, it will treat extra occurance as empty String in the resulting array. For example:
def sampleText = "AxxBxxxC"
println sampleText.split('x')

Since there is an extra x before B, there will be one empty String between A and B. And because there are two extra x after B, there will be two extra empty String after B in the resulting array. The output will be:

[A, , B, , , C]

Which will behave the same if we replace x with space. Example:

def sampleText = "A  B   C"
println sampleText.split(' ')
def sampleText = "A\n\nB\n\n\nC"
println sampleText.split('\n')
Will give the same result:
[A, , B, , , C]

Groovy Split() with Regular Expression

Groovy String's split() method can also take a regular expression as a delimiter. Here are some examples:

Split by a single digit:

def sampleText = "A1B23C456D"
println sampleText.split(/\d/)

Since extra occurance of delimiter will yield empty Strings, the code will render:

[A, B, , C, , , D]

Split by 2 digits:

def sampleText = "A1B23C456D"
println sampleText.split(/\d\d/)

Since 23 and 45 are the only consecutive two digits, we will have the result:

[A1B, C, 6D]

Split by any number of digits:

def sampleText = "A1B23C456D"
println sampleText.split(/\d+/)

Will split the String with any length of digit sequence, hence the expected output:

[A, B, C, D]