Joins all items in the `collection` into a `string`, separated by the given `delimiter`. - Android java.lang

Android examples for java.lang:String Join

Description

Joins all items in the `collection` into a `string`, separated by the given `delimiter`.

Demo Code

import java.util.Collection;
import java.util.Iterator;

public class Main{

    /**/*from   w  ww.  j  a  v a  2s  .c  o m*/
     * Joins all items in the `collection` into a `string`, separated by the given `delimiter`.
     * 
     * @param collection
     *            the collection of items
     * @param delimiter
     *            the delimiter to insert between each item
     * @return the string representation of the collection of items
     */
    public static String join(Collection<?> collection, String delimiter) {
        if (collection == null)
            return "";

        StringBuilder buffer = new StringBuilder();
        Iterator<?> i = collection.iterator();
        while (i.hasNext()) {
            buffer.append(i.next());
            if (i.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }

}

Related Tutorials