Usually written like this:
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); }
Simpler way of writing:
public static void main(String[] args) { String[] arr = new String[]{"aa", "cc", "bb"}; // 转换前的字符串数组 String strs = String.join(",", arr); // 转换后的逗号分隔字符串 System.out.println(strs); }
The above is the detailed content of How to convert string array to comma separated string in Java. For more information, please follow other related articles on the PHP Chinese website!