Java Iterable Join join(Iterable strings, String delimiter)

Here you can find the source of join(Iterable strings, String delimiter)

Description

Joins a list strings into a single string.

License

LGPL

Parameter

Parameter Description
strings The list of strings
delimiter The delimiter between strings

Return

The joined string

Declaration

public static String join(Iterable<String> strings, String delimiter) 

Method Source Code

//package com.java2s;
/**/* ww w.  j  a  va  2  s. c  o m*/
 * Copyright 2011-2016 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

import java.util.Iterator;

public class Main {
    /**
     * Joins a list strings into a single string.
     * 
     * @param strings
     *        The list of strings
     * @param delimiter
     *        The delimiter between strings
     * @return The joined string
     */
    public static String join(String[] strings, String delimiter) {
        StringBuilder r = new StringBuilder();
        for (int i = 0, length = strings.length - 1; i <= length; i++) {
            r.append(strings[i]);
            if (i < length)
                r.append(delimiter);
        }
        return r.toString();
    }

    /**
     * Joins a list strings into a single string.
     * 
     * @param strings
     *        The list of strings
     * @param delimiter
     *        The delimiter between strings
     * @return The joined string
     */
    public static String join(Iterable<String> strings, String delimiter) {
        StringBuilder r = new StringBuilder();
        for (Iterator<String> i = strings.iterator(); i.hasNext();) {
            r.append(i.next());
            if (i.hasNext())
                r.append(delimiter);
        }
        return r.toString();
    }
}

Related

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