Java Iterable to String toString(Iterable objects, String sep)

Here you can find the source of toString(Iterable objects, String sep)

Description

Returns a string that contains all objects separated with the given separator.

License

Open Source License

Parameter

Parameter Description
objects an iterable of objects
sep a separator string

Return

a string that contains all objects separated with the given separator

Declaration

public static String toString(Iterable<? extends Object> objects,
        String sep) 

Method Source Code

//package com.java2s;
import java.util.Iterator;

public class Main {
    /**//from   w w  w  .  j  a  v a  2s. co  m
     * Returns a string that contains all objects separated with the given
     * separator.
     * 
     * @param objects
     *            an iterable of objects
     * @param sep
     *            a separator string
     * @return a string that contains all objects separated with the given
     *         separator
     */
    public static String toString(Iterable<? extends Object> objects,
            String sep) {
        StringBuilder builder = new StringBuilder();
        Iterator<? extends Object> it = objects.iterator();
        if (it.hasNext()) {
            builder.append(it.next());
            while (it.hasNext()) {
                builder.append(sep);
                builder.append(it.next());
            }
        }

        return builder.toString();
    }
}

Related

  1. iterableToString(Iterable charSequenceIterable)
  2. iterableToString(Iterable itrbl)
  3. iterableToString(Iterable objects, int limit)
  4. toString(final Iterable iterable, final CharSequence delimiter)
  5. toString(Iterable c, String delim, String prefix, String suffix)
  6. toString(Iterable it, String separator)
  7. toString(Iterable names)
  8. toString(Iterable iterable)
  9. toString(Iterable strs)