Here you can find the source of implodeCollection(Collection> items, String prefix, String suffix, String delimiter, String lastItemSuffix)
Parameter | Description |
---|---|
items | the items to glue together |
prefix | an optional string to prepend to the result string |
suffix | an optional string to append to the result string |
delimiter | the glue to insert between adjacent items |
lastItemSuffix | an optional spacer to add between the last element and <b>suffix</b>, if there is at least one element |
Parameter | Description |
---|---|
NullPointerException | if <b>items</b> is null |
public static String implodeCollection(Collection<?> items, String prefix, String suffix, String delimiter, String lastItemSuffix)
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.Iterator; public class Main { /**/*w ww.j a v a 2s . com*/ * Glue together all <b>items</b> into one String. <b>prefix</b> is prepended (once) to the list, and <b>suffix</b> * is appended (once) to it, if they are non-{@code null}. If <b>delimiter</b> is non-{@code null}, it is inserted * between each two elements of <b>items</b> (otherwise they are appended directly to one another). If there is at * least one element in <b>items</b> and <b>lastItemSuffix</b> is non-{@code null}, it is appended after the last * element and before <b>suffix</b>. * * A {@link StringBuilder} is used to construct the result string, which takes care of converting all primitive * types, and relies on {@link Object#toString()} for all complex types. * * * @param items * the items to glue together * @param prefix * an optional string to prepend to the result string * @param suffix * an optional string to append to the result string * @param delimiter * the glue to insert between adjacent items * @param lastItemSuffix * an optional spacer to add between the last element and <b>suffix</b>, if there is at least one element * @return the resulting string * * @throws NullPointerException * if <b>items</b> is {@code null} */ public static String implodeCollection(Collection<?> items, String prefix, String suffix, String delimiter, String lastItemSuffix) { if (items == null) throw new NullPointerException("items argument may not be null"); if (prefix == null) prefix = ""; if (suffix == null) suffix = ""; if (delimiter == null) delimiter = ""; if (lastItemSuffix == null) lastItemSuffix = ""; StringBuilder sb = new StringBuilder(prefix); if (!items.isEmpty()) { Iterator<?> it = items.iterator(); for (int i = 0; i < items.size() - 1; ++i) sb.append(it.next()).append(delimiter); sb.append(it.next()).append(lastItemSuffix); } return sb.append(suffix).toString(); } }