Java Iterable Join join(String separator, Iterable elements)

Here you can find the source of join(String separator, Iterable elements)

Description

Joins a collection of elements into a single string value, with a specified separator.

License

CDDL license

Parameter

Parameter Description
separator the separator to place between joined elements.
elements the collection of strings to be joined.

Return

the string containing the joined elements.

Declaration

public static String join(String separator, Iterable<?> elements) 

Method Source Code

//package com.java2s;
/*/*from ww  w .  j  a v a2s .co m*/
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2009 Sun Microsystems Inc.
 * Portions Copyright 2010-2011 ApexIdentity Inc.
 * Portions Copyright 2011-2016 ForgeRock AS.
 */

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

public class Main {
    /**
     * Joins a collection of elements into a single string value, with a specified separator.
     *
     * @param separator the separator to place between joined elements.
     * @param elements the collection of strings to be joined.
     * @return the string containing the joined elements.
     */
    public static String join(String separator, Iterable<?> elements) {
        StringBuilder sb = new StringBuilder();
        for (Iterator<?> i = elements.iterator(); i.hasNext();) {
            sb.append(i.next());
            if (i.hasNext() && separator != null) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }

    /**
     * Joins an array of strings into a single string value, with a specified separator.
     *
     * @param separator the separator to place between joined elements.
     * @param elements the array of strings to be joined.
     * @return the string containing the joined string array.
     */
    public static String join(String separator, Object... elements) {
        return join(separator, Arrays.asList(elements));
    }
}

Related

  1. join(String delimiter, Iterable stringsIterable)
  2. join(String delimiter, String wrap, Iterable objs)
  3. join(String glue, Iterable pieces)
  4. join(String glue, Iterable tokens)
  5. join(String sep, Iterable strings)
  6. join(String separator, Iterable objects)
  7. join(String separator, Iterable args)
  8. join(String seperator, Iterator objects)
  9. join(String seprator, Iterable coll)