我有 Set
&想将其转换为逗号分隔的字符串.我的方法如下所示,但也在寻找其他意见.
I have Set<String> result
& would like to convert it to comma separated string. My approach would be as shown below, but looking for other opinion as well.
List<String> slist = new ArrayList<String> (result);
StringBuilder rString = new StringBuilder();
Separator sep = new Separator(", ");
//String sep = ", ";
for (String each : slist) {
rString.append(sep).append(each);
}
return rString;
Java 8 起:
String.join(",", slist);
来自 Apache Commons 库:
From Apache Commons library:
import org.apache.commons.lang3.StringUtils
用途:
StringUtils.join(slist, ',');
另一个类似的问答这里
这篇关于在java中将列表转换为逗号分隔字符串的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!