Split a string in groovy

You can use the Java split(regex) method to achieve your first goal and then groovy syntactic sugar to help with the rest:

def str = ",,,,,"

def arr = str.split(/,/, -1)

println arr.size() // 6

arr[0] = 1
arr[1] = 2
arr[2] = 3

println arr // [1, 2, 3, , , ]

See also Groovy split csv and empty fields

Leave a Comment