stringlistgroovyconcatenation

How to print Groovy list and keep the quotes?


We have a list:

List test = ["a", "b", "c"]

I don't want to alter this list hardcoded, since it has many items.

When printing this like:

println "${test}"

We get [a, b, c] but I want to have ["a", "b", "c"]

Any suggestions?


Solution

  • You can try representing your list as String by joining all elements like this:

    List test = ["a", "b", "c"]
    
    String listAsString = "[\"${test.join('", "')}\"]"
    
    println listAsString
    

    Output

    ["a", "b", "c"]
    

    It join all elements using ", " and adds [" in the beginning and "] in the end of the string.