join string array with passed in separator - Android java.lang

Android examples for java.lang:String Join

Description

join string array with passed in separator

Demo Code


public class Main{

    public static String join(String[] array, String sep) {
        if (array == null) {
            return null;
        }//ww w .j  a v a 2  s.  co m

        int arraySize = array.length;
        int sepSize = 0;
        if (sep != null && !sep.equals("")) {
            sepSize = sep.length();
        }

        int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16
                : array[0].length()) + sepSize) * arraySize);
        StringBuilder buf = new StringBuilder(bufSize);

        for (int i = 0; i < arraySize; i++) {
            if (i > 0) {
                buf.append(sep);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

}

Related Tutorials