views:

47

answers:

2

Hi, I'm using Groovy and Grails and am trying to take a parameter passed to a controller, parse it, and add each individual element into a list. I thought this would work, but it is adding the whole string into the list, leaving me with only one element.

list = []

list.add(params["firstNames"].split())

is returning a list with size 1, with the list element being a string containing all the names.

also, if I do list = params["firstNames"].split()) , it is showing a size of 2 (i have two elements) but it is still treating it as a String and I cannot perform any other list operations on it.

what is it that I'm doing wrong?

thanks for the help.

A: 

how is your string delimited? may be you need to pass the delimiter to the split function.

Ibrahim
A: 

Try a variation of this:

    String foo = 'foo,bar,baz'
    def list = foo.split(',') as List

    assert list instanceof java.util.List
    assert list.size() == 3

The key part is the as List. If this doesn't work for you, make sure you're using the correct delimiter argument to split(). If you can provide us with an example of what the parameter value might be, we can probably provide a better answer.

Rob Hruska