Solved

How to convert a string to list in Groovy

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 80
micro services vs rest web services 16 87
backtracking recursion  code 19 54
diffSum example 4 27
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…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

864 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

22 Experts available now in Live!

Get 1:1 Help Now