Java Iterable Join join(Iterable source, String separator)

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

Description

Join an Iterable of String into one string with a separator

License

Open Source License

Parameter

Parameter Description
source a parameter
separator a parameter

Declaration

public static String join(Iterable<String> source, String separator) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

public class Main {
    /**/*  w w  w  . j a va2s .  c o m*/
     * Join an Iterable of String into one string with a separator
     * @param source
     * @param separator
     */
    public static String join(Iterable<String> source, String separator) {

        Iterator<String> iterator = null;
        if (source == null) {
            return "";
        }

        iterator = source.iterator();
        if (!(iterator.hasNext())) {
            return "";
        }

        StringBuilder builder = new StringBuilder(iterator.next());
        while (iterator.hasNext()) {
            builder.append(separator).append(iterator.next());
        }
        return builder.toString();
    }
}

Related

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