Java Iterable Join join(Iterable iterable, String delimiter)

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

Description

Joins content of any Iterable object into string with specified delimiter.

License

Apache License

Parameter

Parameter Description
iterable object we can iterate with string values
delimiter string used as values splitter

Return

joined string or empty string

Declaration

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

Method Source Code


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

import java.util.*;

public class Main {
    /**//from  w  ww .  j a  v  a  2s  . com
     * Joins content of any Iterable<String> object into string with specified delimiter.
     * If null or empty object is provided, empty string is returned.
     *
     * @param iterable object we can iterate with string values
     * @param delimiter string used as values splitter
     * @return joined string or empty string
     */
    public static String join(Iterable<String> iterable, String delimiter) {
        if (iterable == null)
            return "";
        Iterator<String> i = iterable.iterator();
        StringBuilder builder = new StringBuilder();
        // append first
        if (i.hasNext())
            builder.append(i.next());
        // append rest
        while (i.hasNext()) {
            builder.append(delimiter).append(i.next());
        }
        if (builder.length() > 0)
            return builder.toString();
        return "";
    }
}

Related

  1. join(Iterable parts, String delimiter)
  2. join(Iterable target, String separator)
  3. join(Iterable iterable, String separator)
  4. join(Iterable array)
  5. join(Iterable items, String separator)
  6. join(Iterable s, String delimiter)
  7. join(Iterable s, String delimiter)
  8. join(Iterable s, String delimiter)
  9. join(Iterable source, char delimiter)