Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 561
Swifting Avatar asked Oct 25 '17 10:10

Swifting


2 Answers

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.

like image 72
Szymon Stepniak Avatar answered Oct 26 '22 13:10

Szymon Stepniak


Groovy has inspect() for better output (closer to input, but be aware, that this is no proper way to serialize Groovy datastructures):

Groovy Shell (2.5.0-beta-1, JVM: 1.8.0_152)
Type ':help' or ':h' for help.
----------------------------------------------------------------------------------------------
groovy:000> test = ["a", "b", "c"]
===> [a, b, c]
groovy:000> test.inspect()
===> ['a', 'b', 'c']
like image 43
cfrick Avatar answered Oct 26 '22 13:10

cfrick