Here you can find the source of join(final Collection e, final String s)
Parameter | Description |
---|---|
e | Collection to join |
s | Separator String |
public static String join(final Collection e, final String s)
//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); } }