Convert to Comma Separated String - Android java.lang

Android examples for java.lang:String Join

Description

Convert to Comma Separated String

Demo Code


import java.util.List;

public class Main {

  public static String toCommaSeparatedString(String spaceSeparatedString) {
    String s = spaceSeparatedString.trim();
    return s.replaceAll(" ", ",");
  }/*from  ww  w .j av  a2  s  .  c om*/

  public static String toCommaSeparatedString(List<String> list) {
    StringBuilder sb = new StringBuilder();
    int size = list.size();
    for (int i = 0; i < size; i++) {
      sb.append(list.get(i));
      if (i < size - 1) {
        sb.append(",");
      }
    }
    return sb.toString();
  }

}

Related Tutorials