Java Iterable Join join(Iterable iterable, String delimiter)

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

Description

join

License

Open Source License

Declaration

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

Method Source Code

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

import java.util.Iterator;

public class Main {
    public static String join(Iterable<? extends Object> iterable, String delimiter) {
        return join(iterable, delimiter, "", "");
    }/*from  w w w . j a  v  a2 s .  c o  m*/

    public static String join(Iterable<? extends Object> iterable, String delimiter, String prepend,
            String append) {

        StringBuilder buffer = new StringBuilder();
        Iterator<?> iterator = iterable.iterator();
        boolean hasNext = iterator.hasNext();

        while (hasNext) {

            buffer.append(prepend);
            buffer.append(String.valueOf(iterator.next()));
            buffer.append(append);

            if (iterator.hasNext()) {
                buffer.append(delimiter);
            } else {
                hasNext = false;
            }
        }

        return buffer.toString();
    }
}

Related

  1. join(final Iterable... iterables)
  2. join(final String delimiter, final Iterable objs)
  3. join(Iterable elements, String delimiter)
  4. join(Iterable iterable, String delimiter)
  5. join(Iterable s, String delimiter)
  6. join(Iterable joinee, String joiner)
  7. join(Iterable c, String delimeter)
  8. join(Iterable elements, String separator)
  9. join(Iterable elements, String separator)