Here you can find the source of join(final Iterable extends Object> objects, final CharSequence separator)
Parameter | Description |
---|---|
objects | collection of Objects to join |
separator | separator to use |
public static String join(final Iterable<? extends Object> objects, final CharSequence separator)
//package com.java2s; import java.util.Iterator; public class Main { /**/*from w ww . j ava 2 s. c o m*/ * Joins the given collection of Objects using the given * separator and each Object's normal toString() method. * <p> * For example, joining the collection "a", "b", "c" with "/" * gives "a/b/c". * * @param objects collection of Objects to join * @param separator separator to use * @return objects joined using the given separator. */ public static String join(final Iterable<? extends Object> objects, final CharSequence separator) { StringBuilder result = new StringBuilder(); for (Iterator<? extends Object> iter = objects.iterator(); iter.hasNext();) { result.append(iter.next().toString()); if (iter.hasNext()) { result.append(separator); } } return result.toString(); } /** * Same as {@link #join(Iterable, CharSequence)} but simply takes an array * of Objects. * * @param objects array of Objects to join * @param separator separator to use */ public static String join(final Object[] objects, final CharSequence separator) { return join(objects, separator, 0, objects.length); } /** * Version of {@link #join(Object[], CharSequence)} that allows you to pass in * a {@link StringBuilder} that the result will be built up in. This is useful if you need * to do add in other stuff later on. * * @param resultBuilder StringBuilder to append results to * @param objects array of Objects to join * @param separator separator to use */ public static void join(final StringBuilder resultBuilder, final Object[] objects, final CharSequence separator) { join(resultBuilder, objects, separator, 0, objects.length); } /** * Version of {@link #join(Object[], CharSequence)} that allows you to specify a range of * indices in the array to join. This can be useful in some cases. * * @param objects array of Objects to join * @param separator separator to use * @param startIndex first index to join * @param endIndex index after last one to join */ public static String join(final Object[] objects, final CharSequence separator, final int startIndex, final int endIndex) { StringBuilder result = new StringBuilder(); join(result, objects, separator, startIndex, endIndex); return result.toString(); } /** * Version of {@link #join(Object[], CharSequence, int, int)} that allows you to pass in * a {@link StringBuilder} that the result will be built up in. This is useful if you need * to do add in other stuff later on. * * @param resultBuilder StringBuilder to append results to * @param objects array of Objects to join * @param separator separator to use * @param startIndex first index to join * @param endIndex index after last one to join */ public static void join(final StringBuilder resultBuilder, final Object[] objects, final CharSequence separator, final int startIndex, final int endIndex) { boolean hasDoneFirst = false; for (int i = startIndex; i < endIndex; i++) { if (hasDoneFirst) { resultBuilder.append(separator); } resultBuilder.append(objects[i].toString()); hasDoneFirst = true; } } }