[groovy-user] Convert String "[1, 2, 3, 4, 5]" to ArrayList

8,572 views
Skip to first unread message

Dean Del Ponte

unread,
May 5, 2010, 3:55:34 PM5/5/10
to us...@groovy.codehaus.org
What's the grooviest way to convert the String "[1, 2, 3, 4, 5]" to an ArrayList?

Thanks!

Dean

--
You received this message because you are subscribed to the Google Groups "Groovy Users" group.
To post to this group, send email to groov...@googlegroups.com.
To unsubscribe from this group, send email to groovy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/groovy-user?hl=en.

Mick Knutson

unread,
May 5, 2010, 3:59:34 PM5/5/10
to us...@groovy.codehaus.org
Do you mean:

ArrayList list = [1, 2, 3, 4, 5]

---
Thank You…

Mick Knutson, President

BASE Logic, Inc.
Enterprise Architecture, Design, Mentoring & Agile Consulting
p. (866) BLiNC-411: (254-6241-1)
f. (415) 685-4233

Website: http://www.baselogic.com
Blog: http://www.baselogic.com/blog/
Linked IN: http://linkedin.com/in/mickknutson
Twitter: http://twitter.com/mickknutson
Vacation Rental: http://tahoe.baselogic.com
---




On Wed, May 5, 2010 at 3:55 PM, Dean Del Ponte <dean.d...@gmail.com> wrote:
[1, 2, 3, 4, 5]

Nagelberg, Kallin

unread,
May 5, 2010, 4:03:41 PM5/5/10
to us...@groovy.codehaus.org

How about

 

def var = Eval.me("[1,2,3]")

assert var.class == ArrayList

 

 

 


Dean Del Ponte

unread,
May 5, 2010, 4:04:34 PM5/5/10
to us...@groovy.codehaus.org
Sadly, no.  I'm being passed a String.  The String is "[1, 2, 3, 4, 5]".

I would like to convert it into the equivalent ArrayList of [1, 2, 3, 4, 5]

String s = "[1, 2, 3, 4, 5]"

ArrayList a = s converted to ArrayList

Does that help to clear it up?

- Dean

Dean Del Ponte

unread,
May 5, 2010, 4:06:31 PM5/5/10
to us...@groovy.codehaus.org
Thanks Kallin,

That worked great!  Do you know of hand what the security implications are of using Eval?  The string is being passed to me from a client's web browser.

- Dean

Nagelberg, Kallin

unread,
May 5, 2010, 4:11:23 PM5/5/10
to us...@groovy.codehaus.org

NP Dean. I suppose the security implications are pretty big in that case. Try running Eval.me("System.exit(0)") in the groovyConsole and you’ll see what I mean. You might want to have the client pass a JSON string instead which could be easily parsed into a list, and will throw an exception if it’s invalid json.

 

-Kal

Alexander Veit

unread,
May 5, 2010, 4:17:26 PM5/5/10
to us...@groovy.codehaus.org
> That worked great! Do you know of hand what the security
> implications are of using Eval? The string is being passed
> to me from a client's web browser.

Then you have to validate the syntax of the input before evaluating it.

-Alex


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Nagelberg, Kallin

unread,
May 5, 2010, 4:18:08 PM5/5/10
to us...@groovy.codehaus.org

Or if you know it’s always going to be a list like that you could do something ugly like

 

def clientString = "[1,2,3,4,5]"

 

ArrayList myList = clientString.replaceAll("\\[","").replaceAll("]","").split(",")

 

assert myList.class == ArrayList

 

myList.each{

    println "${it} [${it.class}]"

}


Alexander Veit

unread,
May 5, 2010, 4:37:40 PM5/5/10
to us...@groovy.codehaus.org
> > That worked great! Do you know of hand what the security
> > implications are of using Eval? The string is being passed
> > to me from a client's web browser.
>
> Then you have to validate the syntax of the input before
> evaluating it.

Or do sth. like this:

"[1, 2, 3, 4, 5, 6]"[1..-2].tokenize(',')*.toInteger()

Nagelberg, Kallin

unread,
May 5, 2010, 4:39:51 PM5/5/10
to us...@groovy.codehaus.org
Nice, I didn't know about that negative index on the range!

-Kal

-----Original Message-----
From: Alexander Veit [mailto:alexand...@gmx.net]
Sent: Wednesday, May 05, 2010 4:38 PM
To: us...@groovy.codehaus.org

Jason Stell

unread,
May 5, 2010, 4:44:01 PM5/5/10
to us...@groovy.codehaus.org
Or if you know it's a list of integers, how about this?:

def clientString = "[1,2,3,4,5]"
ArrayList<Integer> myList = clientString.replaceAll(/\D/,' ').split()*.toInteger()

Cheers
Jason

On Wed, May 5, 2010 at 3:18 PM, Nagelberg, Kallin <KNage...@globeandmail.com> wrote:

Or if you know it’s always going to be a list like that you could do something ugly like

 

def clientString = "[1,2,3,4,5]"

 

ArrayList myList = clientString.replaceAll("\\[","").replaceAll("]","").split(",").

Dean Del Ponte

unread,
May 5, 2010, 4:45:28 PM5/5/10
to us...@groovy.codehaus.org
Great help everyone!  Thanks!

David Herman

unread,
May 5, 2010, 5:12:30 PM5/5/10
to us...@groovy.codehaus.org
It looks like you're just being passed json so you should be able to do JSON.parse(theString) 
Reply all
Reply to author
Forward
0 new messages