问题:
JavaScript has Array.join()
JavaScript具有Array.join()
js>["Bill","Bob","Steve"].join(" and ")
Bill and Bob and Steve
Does Java have anything like this? Java有这样的东西吗? I know I can cobble something up myself with StringBuilder: 我知道我可以使用StringBuilder自己整理一些东西:
static public String join(List<String> list, String conjunction)
{
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String item : list)
{
if (first)
first = false;
else
sb.append(conjunction);
sb.append(item);
}
return sb.toString();
}
...but there's no point in doing this if something like it is already part of the JDK. ...但是如果类似的东西已经成为JDK的一部分,那么这样做是没有意义的。
解决方案:
参考一: https://stackoom.com/question/7LjY/Java-转换列表-String-到一个字符串参考二: https://oldbug.net/q/7LjY/Java-convert-List-String-to-a-String
来源:oschina
链接:https://my.oschina.net/u/4437884/blog/4282433