Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

groovy: change each element of a list and join

I want to wrap each element of a list in single-quotes and join them into a string.

Sample input: ["aa", "bb", "cc"]

Expected output: "'aa', 'bb', 'cc'"

I guessed that this could be done with a collect+closure, so I tried:

def mylist = ["aa", "bb", "cc"]
println mylist.collect{ 'it' }.join(', ')

But the output is: "it, it, it" and this is not what I want.

How can I append and pre-pend a single quote to each element of the list? Any other oneliner (or short) groovy solutions apart from collect and join?

like image 945
nonbeing Avatar asked Dec 19 '12 15:12

nonbeing


1 Answers

You should try

mylist.collect{ "'$it'" }.join(', ')

with 'it' you just return the string "it".

like image 70
micha Avatar answered Oct 30 '22 09:10

micha