Search code examples
groovystring-interpolation

Groovy: List of Strings interpolated into a text loses apostrophes


I would like to create a text in which I interpolate a list of stings ["aa","bb"].

def texts = ["aa" , "bb"]
def out = "my list: $texts"
println out

I want the output to be:

my list: ["aa","bb"]

But the output I am getting is:

my list: [aa, bb]

How can I solve this issue?


Solution

  • The result of texts.toString() does not include the quotation marks. So you have to create that output yourself:

    def out = "my list: ${texts.collect{'"' + it + '"'}}"