Solved

How to convert a string to list in Groovy

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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