简体   繁体   English

如何在Groovy中将列表转换为字符串并删除括号而不使用替换?

[英]How to convert list to string in Groovy and remove brackets without using replace?

So I have the following code where I add some elements to a 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 string has opening and closing brackets ('[', ']') which I don't want ("I wish my co workers were [Dwight Shrute, Jim Halpert, Toby Flenderson]"). 结果字符串有开口和右括号('[',']'),我不想要(“我希望我的同事们是[Dwight Shrute,Jim Halpert,Toby Flenderson]”)。

It seems one way to remove those brackets is to use String's replace function as suggested here . 似乎删除这些括号的一种方法是使用String的替换函数,如此处所示

But what if in the actual list I am using there are already brackets in the values, they would get replaced also. 但是,如果在我使用的实际列表中已经在值中包含括号,它们也会被替换。

So is there a way to print out this list as a string without having those brackets show up and without using the replace function? 那么有没有办法将这个列表打印成一个字符串而不显示那些括号而不使用替换功能?

使用加入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM