join string by separator - Android java.lang

Android examples for java.lang:String Join

Description

join string by separator

Demo Code

import android.text.Html;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static String join(Object[] array, String separator) {
        if (array == null) {
            return null;
        }/*from  w w  w .  ja  v a  2 s.co  m*/
        return join(array, separator, 0, array.length);
    }
    public static String join(Object[] array, String separator,
            int startIndex, int endIndex) {
        if (array == null) {
            return null;
        }
        if (separator == null) {
            separator = "";
        }

        int noOfItems = (endIndex - startIndex);
        if (noOfItems <= 0) {
            return "";
        }

        StringBuilder buf = new StringBuilder(noOfItems * 16);

        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

}

Related Tutorials