implode string from collection and array - Android java.lang

Android examples for java.lang:String Join

Description

implode string from collection and array

Demo Code

import java.lang.reflect.Array;
import java.net.URL;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class Main{

    public static String implode(final Collection<?> values,
            final String delimiter) {
        return implode(values.toArray(), delimiter);
    }/*from  w  ww.j ava  2 s  . co m*/
    public static String implode(final Object[] values,
            final String delimiter) {
        if (values == null)
            return "";

        final StringBuilder out = new StringBuilder();

        for (int i = 0, l = values.length; i < l; i++) {
            if (i > 0)
                out.append(delimiter);
            out.append(values[i]);
        }
        return out.toString();
    }
    public static String implode(final Object[] values,
            final String delimiter, int start, int end) {
        if (values == null)
            return "";

        final StringBuilder out = new StringBuilder();

        int l = end > values.length - 1 ? values.length : end + 1;
        for (int i = start; i < l; i++) {
            if (i > start)
                out.append(delimiter);
            out.append(values[i]);
        }
        return out.toString();
    }
    public static String implode(final long[] values,
            final String delimiter, int start, int end) {
        if (values == null)
            return "";

        final StringBuilder out = new StringBuilder();

        int l = end > values.length - 1 ? values.length : end + 1;
        for (int i = start; i < l; i++) {
            if (i > start)
                out.append(delimiter);
            out.append(values[i]);
        }
        return out.toString();
    }

}

Related Tutorials