Java Iterable Join join(Iterable array, String joiner)

Here you can find the source of join(Iterable array, String joiner)

Description

Missing counterpart for String.split()

License

Apache License

Parameter

Parameter Description
array a parameter
joiner The delimiter to add between each element

Return

The joined string

Declaration

public static <T> String join(Iterable<T> array, String joiner) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;

public class Main {
    /**//from  w w  w.  ja  va  2s .c om
     * Missing counterpart for String.split()
     *
     * @param array
     * @param joiner The delimiter to add between each element
     * @return The joined string
     */
    public static <T> String join(Iterable<T> array, String joiner) {
        StringBuilder b = new StringBuilder();
        Iterator<T> i = array.iterator();
        boolean first = true;

        while (i.hasNext()) {
            if (first)
                first = false;
            else
                b.append(joiner);
            b.append(i.next());
        }

        return b.toString();
    }
}

Related

  1. join(Iterable source, char delimiter)
  2. join(Iterable source, String separator)
  3. join(Iterable strings)
  4. join(Iterable strings, String delimiter)
  5. join(Iterable strings, String separator)
  6. join(Iterable coll, String sep)
  7. join(Iterable coll, String sep)
  8. join(Iterable coll, String sep)
  9. join(Iterable coll, String separator)