Java Iterable toFriendlyString(Iterable iterable, String seperator)

Here you can find the source of toFriendlyString(Iterable iterable, String seperator)

Description

Turns every element of the iterable into a friendly string which can be used in a statement

License

Open Source License

Parameter

Parameter Description
iterable The Iterable of elements
seperator The seperator between every element

Declaration

public static String toFriendlyString(Iterable<?> iterable, String seperator) 

Method Source Code

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

import java.util.Iterator;

public class Main {
    /**/*w  w w . j  a va 2 s  .c  o m*/
     * Turns every element of the iterable into a friendly string which can be used in a statement
     * 
     * @param iterable The Iterable of elements
     * @param seperator The seperator between every element
     */
    public static String toFriendlyString(Iterable<?> iterable, String seperator) {
        Iterator<?> iter = iterable.iterator();
        StringBuilder builder = new StringBuilder();

        while (iter.hasNext()) {
            Object next = iter.next();

            builder.append(next);
            if (iter.hasNext())
                builder.append(seperator);
        }

        return builder.toString();
    }

    /**
     * Turns every element of the Array into a friendly string which can be used in a statement
     * 
     * @param o The Array of elements
     * @param seperator The seperator between every element
     */
    public static String toFriendlyString(Object[] o, String seperator) {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < o.length; i++) {
            Object next = o[i];

            builder.append(next);
            if (o.length >= i + 2)
                builder.append(seperator);
        }

        return builder.toString();
    }
}

Related

  1. removeElement(final Iterable iterable, int index)
  2. removeIgnoreCase(Iterable haystack, String needle)
  3. secondOf(final Iterable iterable)
  4. sequenceEqual(Iterable one, Iterable two)
  5. shallowCopy(Iterable ori)
  6. unique(Iterable source, T defaultElement)
  7. valueOfMultiple(Iterable... iterables)