Solved

How to convert a string to list in Groovy

Posted on 2009-04-13
4
2,926 Views
Last Modified: 2013-11-13
Hi experts,
  I have a requirement to convert a "list-like" string into a real list in Groovy. For example, if I have a string as '[1,2,3,4,5]', how can I write a closure to convert it into a list type as [1,2,3,4,5]? If possible, could you please show me the real code?
  I'm totally new to Groovy and learning it right now.
  Thanks!

Cheers,
Michael
0
Comment
Question by:michaelzhou23
  • 2
4 Comments
 
LVL 10

Expert Comment

by:mahome
ID: 24135486
There are likely many of possibilities in groovy. Here is one of them:

// Closure
def convertToList = { it[1..-2].split(',').collect{ it } }
 
// Example-Usage
def string = "[1,2,3,4,5]"
def result = convertToList(string)
 
println result
println result.getClass()
        

Open in new window

0
 
LVL 35

Expert Comment

by:TimYates
ID: 24140212
This will return them as ints...

But as mahome says, there's loads of ways round this
def convertInts = { list ->
  list.tokenize( "[, ]" ).collect { it as Integer }  
}
def iList = convertInts( "[1, 2, 3, 4, 5, 11]" )
iList.each { println "$it (${it.class})" }

Open in new window

0
 

Author Comment

by:michaelzhou23
ID: 24143634
Hi Thanks a lot!
I tested them and they worked. But I was wondering whether there is any way that I can put the whole things in one statement? I mean i would need this in a groovy expression.
Thanks!
0
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 24145113
?
"[1,2,3,4,5]".tokenize( "[, ]" ).collect { it as Integer }

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question