0){sb.append(",");}sb"/> 0){sb.append(",");}sb">

Home  >  Article  >  Java  >  How to convert string array to comma separated string in Java

How to convert string array to comma separated string in Java

PHPz
PHPzforward
2023-04-27 11:49:084663browse

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete