Java Collection Join join(final Collection e, final String s)

Here you can find the source of join(final Collection e, final String s)

Description

Join a collection as a string

License

Open Source License

Parameter

Parameter Description
e Collection to join
s Separator String

Return

E.cont#toString separated by s

Declaration

public static String join(final Collection e, final String s) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**/*  w ww .ja v  a  2s . com*/
     * Join a collection as a string
     *
     * @param e
     *            Collection to join
     * @param s
     *            Separator String
     * @return E.cont#toString separated by s
     */
    public static String join(final Collection e, final String s) {
        if (e == null || e.size() == 0)
            return "";
        final StringBuilder builder = new StringBuilder();
        for (final Object o : e)
            builder.append(o).append(s);
        final String r = builder.toString();
        return r.substring(0, r.length() - s.length());
    }

    /**
     * Join a collection as a string
     *
     * @param e
     *            Collection to join
     * @param s
     *            Separator (when more than one item left)
     * @param l
     *            Separator (before last item)
     * @return Joined String
     */
    public static String join(final Collection e, final String s, final String l) {
        if (e == null || e.size() == 0)
            return "";
        final StringBuilder sb = new StringBuilder();
        final Iterator it = e.iterator();
        Object o;
        boolean first = true;
        for (;;) {
            o = it.next();
            if (!it.hasNext())
                if (first)
                    return o.toString();
                else
                    return sb.append(l).append(o).toString();
            if (first)
                first = false;
            else
                sb.append(s);
            sb.append(o);
        }
    }

    /**
     * Join an object array
     *
     * @param e
     *            Object array
     * @param s
     *            String separator
     * @return string
     * @see #join(java.util.Collection, String)
     */
    public static String join(final Object[] e, final String s) {
        return join(Arrays.asList(e), s);
    }
}

Related

  1. join(Collection s, String delimiter)
  2. join(Collection things, String delim)
  3. join(Collection values, String join)
  4. join(final char _separator, final boolean _quotes, final Collection _list, final String _emptyString)
  5. join(final Collection collection, String delimiter)
  6. join(final Collection items, final String glue)
  7. join(final Collection coll, final String delimiter)
  8. join(final Collection coll, final String glue)
  9. join(final Collection items, final String inBetween)