Groovy: change each list item and concatenate

I want to wrap each list item in single quotes and concatenate them into a string.

Input Example: ["aa", "bb", "cc"]

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

I figured that this could be done using closure +, so I tried:

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

But the conclusion is: "it, it, it" , and that is not what I want.

How can I add and pre-set a single quote for each list item? Any other oneliner (or short) groovy solutions other than collecting and combining?

+6
source share
1 answer

You must try

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

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

+15
source

All Articles