Java tutorial
//package com.java2s; import android.text.TextUtils; import java.util.List; public class Main { /** * default join separator **/ public static final String DEFAULT_JOIN_SEPARATOR = ","; /** * join list to string, separator is "," * <p/> * <pre> * join(null) = ""; * join({}) = ""; * join({a,b}) = "a,b"; * </pre> * * @param list * @return join list to string, separator is ",". if list is empty, return "" */ public static String join(List<String> list) { return join(list, DEFAULT_JOIN_SEPARATOR); } /** * join list to string * <p/> * <pre> * join(null, '#') = ""; * join({}, '#') = ""; * join({a,b,c}, ' ') = "abc"; * join({a,b,c}, '#') = "a#b#c"; * </pre> * * @param list * @param separator * @return join list to string. if list is empty, return "" */ public static String join(List<String> list, char separator) { return join(list, new String(new char[] { separator })); } /** * join list to string. if separator is null, use {@link #DEFAULT_JOIN_SEPARATOR} * <p/> * <pre> * join(null, "#") = ""; * join({}, "#$") = ""; * join({a,b,c}, null) = "a,b,c"; * join({a,b,c}, "") = "abc"; * join({a,b,c}, "#") = "a#b#c"; * join({a,b,c}, "#$") = "a#$b#$c"; * </pre> * * @param list * @param separator * @return join list to string with separator. if list is empty, return "" */ public static String join(List<String> list, String separator) { return list == null ? "" : TextUtils.join(separator, list); } }