join string from object array and separator - Android java.lang

Android examples for java.lang:String Join

Description

join string from object array and separator

Demo Code

import android.text.TextUtils;
import java.util.Arrays;
import java.util.Iterator;

public class Main{

    public static String join(Object[] elements, CharSequence separator) {
        return join(Arrays.asList(elements), separator);
    }//  w  w  w .j ava2 s.c  om
    public static String join(Iterable<? extends Object> elements,
            CharSequence separator) {
        StringBuilder builder = new StringBuilder();

        if (elements != null) {
            Iterator<? extends Object> iter = elements.iterator();
            if (iter.hasNext()) {
                builder.append(String.valueOf(iter.next()));
                while (iter.hasNext()) {
                    builder.append(separator).append(
                            String.valueOf(iter.next()));
                }
            }
        }

        return builder.toString();
    }

}

Related Tutorials