Possible Duplicates:
What’s the best way to build a string of delimited items in Java?
Java: convert List<String> to a join()d string
在Java中,给定一个集合,获取迭代器并为第一个(或最后一个)元素做一个单独的案例,其余的得到一个逗号分隔的字符串似乎很无聊,有没有类似 str.join
在 Python 中?
为避免它被关闭为重复的额外说明:我宁愿不使用像 Apache Commons 这样的外部库。
谢谢!
几年后更新...
最佳答案
不,没有。这是我的尝试:
/**
* Join a collection of strings and add commas as delimiters.
* @require words.size() > 0 && words != null
*/
public static String concatWithCommas(Collection<String> words) {
StringBuilder wordList = new StringBuilder();
for (String word : words) {
wordList.append(word + ",");
}
return new String(wordList.deleteCharAt(wordList.length() - 1));
}
关于java - Python 的 str.join 在 Java 中是否有直接的等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3236213/