Java 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

License

LGPL

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;
//License from project: LGPL 

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

public class Main {
    /**//  w ww .  j  av a 2 s  . c o  m
     * 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(Collection objects, String glue)
  2. join(Collection paramCollection, String paramString)
  3. join(Collection parts, String sep)
  4. join(Collection s, String delimiter)
  5. join(Collection s, String delimiter, boolean doQuote)
  6. join(Collection strings, String separator)
  7. join(Collection tokens, String separator)
  8. join(Collection var0, Object var1)
  9. join(Collection c, String joinWith)