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?
You should try
mylist.collect{ "'$it'" }.join(', ')
with 'it'
you just return the string "it".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With