How to convert list to string in Groovy and remove brackets without replacement?

So, I have the following code in which I add some items to the list, sort them and print as a string.

def officeWorkers = [] officeWorkers << "Jim Halpert" officeWorkers << "Dwight Shrute" officeWorkers << "Toby Flenderson" officeWorkers.sort() println("I wish my co workers were ${officeWorkers.toString()}") 

The resulting line has opening and closing brackets ('[', ']]') that I do not want ("I would like my employees to be [Dwight Shrut, Jim Halpert, Toby Flenderson]").

It seems that one way to remove these brackets is to use the String replacement function, as suggested here .

But what if the actual list that I use already contains brackets in the values, they will also be replaced.

So, is there a way to print this list as a string without displaying these brackets and without using the replace function?

+5
source share
1 answer

Use join:

 println("I wish my co workers were ${officeWorkers.join(', ')}") 
+9
source

Source: https://habr.com/ru/post/1246491/


All Articles