Android Collection Join join(Collection strings, String sep)

Here you can find the source of join(Collection strings, String sep)

Description

Join a collection of strings by a seperator

Parameter

Parameter Description
strings collection of string objects
sep string to place between strings

Return

joined string

Declaration

public static String join(Collection strings, String sep) 

Method Source Code

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

public class Main {
    /**//from   w w w .  j  a va  2 s.c om
     * Join a collection of strings by a seperator
     * 
     * @param strings
     *            collection of string objects
     * @param sep
     *            string to place between strings
     * @return joined string
     */
    public static String join(Collection strings, String sep) {
        return join(strings.iterator(), sep);
    }

    /**
     * Join a collection of strings by a seperator
     * 
     * @param strings
     *            iterator of string objects
     * @param sep
     *            string to place between strings
     * @return joined string
     */
    public static String join(Iterator strings, String sep) {
        if (!strings.hasNext())
            return "";

        String start = strings.next().toString();
        if (!strings.hasNext()) // only one, avoid builder
            return start;

        StringBuilder sb = new StringBuilder(64).append(start);
        while (strings.hasNext()) {
            sb.append(sep);
            sb.append(strings.next());
        }
        return sb.toString();
    }
}

Related

  1. join(String conjunction, Collection... collections)
  2. suffixAndJoin(String suffix, String delimiter, Collection values)
  3. toString(Collection collection, String separator)
  4. join(Collection collection, String s)
  5. join(Collection strings, String sep)
  6. join(final Collection strings, String delimeter)
  7. join(Collection collection, String delimiter)
  8. join(Collection s, String delimiter)
  9. join(CharSequence separator, Collection values)