Java Collection Join join(String glue, Collection pieces)

Here you can find the source of join(String glue, Collection pieces)

Description

Join a Collection of Strings together.

License

Open Source License

Parameter

Parameter Description
glue Token to place between Strings.
pieces Collection of Strings to join.

Return

String presentation of joined Strings.

Declaration

public final static String join(String glue, Collection pieces) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**// w  ww .  ja  v  a  2s.  c  om
     * Join an Iteration of Strings together.
     *
     * <h5>Example</h5>
     *
     * <pre>
     *   // get Iterator of Strings ("abc","def","123");
     *   Iterator i = getIterator();
     *   out.print( TextUtils.join(", ",i) );
     *   // prints: "abc, def, 123"
     * </pre>
     *
     * @param glue Token to place between Strings.
     * @param pieces Iteration of Strings to join.
     * @return String presentation of joined Strings.
     */
    public final static String join(String glue, Iterator pieces) {
        StringBuffer s = new StringBuffer();

        while (pieces.hasNext()) {
            s.append(pieces.next().toString());

            if (pieces.hasNext()) {
                s.append(glue);
            }
        }

        return s.toString();
    }

    /**
     * Join an array of Strings together.
     *
     * @param glue Token to place between Strings.
     * @param pieces Array of Strings to join.
     * @return String presentation of joined Strings.
     *
     * @see #join(String, java.util.Iterator)
     */
    public final static String join(String glue, String[] pieces) {
        return join(glue, Arrays.asList(pieces).iterator());
    }

    /**
     * Join a Collection of Strings together.
     *
     * @param glue Token to place between Strings.
     * @param pieces Collection of Strings to join.
     * @return String presentation of joined Strings.
     *
     * @see #join(String, java.util.Iterator)
     */
    public final static String join(String glue, Collection pieces) {
        return join(glue, pieces.iterator());
    }
}

Related

  1. join(String delimiter, Collection items)
  2. join(String delimiter, Collection stringCollection)
  3. join(String delimiter, Collection strings)
  4. join(String delimiter, Collection values)
  5. join(String glue, Collection c)
  6. join(String glue, Collection c)
  7. join(String glue, Collection strings)
  8. join(String glue, Collection items)
  9. join(String join_string, Collection c)