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?
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
["a", "b", "c"]
It join all elements using ", "
and adds ["
in the beginning and "]
in the end of the string.
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']
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