nonbeing
nonbeing

Reputation: 7317

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?

Upvotes: 13

Views: 19407

Answers (1)

micha
micha

Reputation: 49572

You should try

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

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

Upvotes: 28

Related Questions