通常会这么写:
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); }
Atas ialah kandungan terperinci Java中如何将字符串数组转换为逗号分隔的字符串. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!