join array to String - Android java.lang

Android examples for java.lang:array join

Description

join array to String

Demo Code

import java.util.Collection;
import java.util.List;
import java.util.Set;

public class Main {

  public static String join(String[] array) {
    return join(array, ",");
  }// w ww  .  ja  v  a  2s.c o m

  public static String join(String[] array, String delim) {
    if (array == null || array.length == 0) {
      return "";
    }
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < array.length; i++) {
      sb.append(array[i]);
      sb.append(delim);
    }
    return sb.substring(0, sb.length() - delim.length());
  }

  public static String join(List<?> list) {
    return join(list, ",");
  }

  public static String join(Set<?> set) {
    return join(set, ",");
  }

  public static String join(Collection<?> collection) {
    return join(collection, ",");
  }

  public static String join(Number[] numbers) {
    return join(numbers, ",");
  }

  public static String join(Number[] numbers, String delim) {
    if (numbers == null || numbers.length == 0) {
      return "";
    }
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < numbers.length; i++) {
      sb.append(numbers[i]);
      sb.append(delim);
    }
    return sb.substring(0, sb.length() - delim.length());
  }

  public static String join(Collection<?> collection, String join) {
    Object[] obj = collection.toArray();
    String[] arr = new String[obj.length];
    for (int i = 0; i < obj.length; i++) {
      arr[i] = String.valueOf(obj[i]);
    }
    return join(arr, join);
  }

}

Related Tutorials