Android Collection Join join(Collection collection, String delimiter)

Here you can find the source of join(Collection collection, String delimiter)

Description

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

License

Open Source License

Parameter

Parameter Description
collection the collection of items
delimiter the delimiter to insert between each item

Return

the string representation of the collection of items

Declaration

public static String join(Collection<?> collection, String delimiter) 

Method Source Code

//package com.java2s;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**//from ww w  .  ja  va 2  s. c om
     * 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<?> iter = collection.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }

    /**
     * Appends the string `append` to `string` separated by the `delimiter` string.
     * 
     * @param string
     *            the string in front
     * @param append
     *            the string to append
     * @param delimiter
     *            the string that separates `string` and `append`
     * @return appended string of `string` + `delimiter` + `append`, or `null` if `string` if `string` is `null`.
     */
    public static String append(final String string, final String append,
            final String delimiter) {
        if (string == null) {
            return append;
        } else {
            final StringBuilder builder = new StringBuilder(string);
            if (delimiter != null)
                builder.append(delimiter);
            if (append != null)
                builder.append(append);
            return builder.toString();
        }
    }
}

Related

  1. toString(Collection collection, String separator)
  2. join(Collection collection, String s)
  3. join(Collection strings, String sep)
  4. join(Collection strings, String sep)
  5. join(final Collection strings, String delimeter)
  6. join(Collection s, String delimiter)
  7. join(CharSequence separator, Collection values)
  8. join(Collection strings, String sep)
  9. write(Collection c, Object separator, Object prefix, Object postfix)