Java Iterable Join join(CharSequence delimiter, Iterable tokens)

Here you can find the source of join(CharSequence delimiter, Iterable tokens)

Description

Returns a string containing the tokens joined by delimiters.

License

Open Source License

Parameter

Parameter Description
tokens an array objects to be joined. Strings will be formed from the objects by calling object.toString().

Declaration

public static String join(CharSequence delimiter, Iterable tokens) 

Method Source Code


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

public class Main {
    /**/*from  w  w w.  j  av  a  2  s .  c  o m*/
     * Returns a string containing the tokens joined by delimiters.
     *
     * @param tokens an array objects to be joined. Strings will be formed from
     *               the objects by calling object.toString().
     */
    public static String join(CharSequence delimiter, Iterable tokens) {
        StringBuilder sb = new StringBuilder();
        Iterator<?> it = tokens.iterator();
        if (it.hasNext()) {
            sb.append(it.next());
            while (it.hasNext()) {
                sb.append(delimiter);
                sb.append(it.next());
            }
        }
        return sb.toString();
    }
}

Related

  1. join(CharSequence delimiter, Iterable tokens)
  2. join(CharSequence delimiter, Iterable elements)
  3. join(CharSequence separator, Iterable strings)
  4. join(final CharSequence sep, final Iterable src)
  5. join(final Iterable elements, final String separator)