Solved

How to convert a string to list in Groovy

Posted on 2009-04-13
4
2,891 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
How Complex Is This Java Course ? 9 63
wordmultiple challenge 12 92
java 8 lambda expresssions exception handling 3 41
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now