这篇文章给大家分享的是有关java如何将字符串数组转换成逗号分隔字符串的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
通常会这么写:
public static void main(String[] args) {
String strs = "";
String[] arr = new String[]{"aa", "cc", "bb"}; // 转换前的字符串数组
StringBuilder sb = new StringBuilder();
for (String ele : arr) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(ele);
}
strs = sb.toString(); // 转换后的逗号分隔字符串
System.out.println(strs);
}
更简单的写法:
public static void main(String[] args) {
String[] arr = new String[]{"aa", "cc", "bb"}; // 转换前的字符串数组
String strs = String.join(",", arr); // 转换后的逗号分隔字符串
System.out.println(strs);
}
感谢各位的阅读!关于“java如何将字符串数组转换成逗号分隔字符串”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!