Java Array Implode implodeArray(Object[] items, String prefix, String suffix, String delimiter, String lastItemSuffix)

Here you can find the source of implodeArray(Object[] items, String prefix, String suffix, String delimiter, String lastItemSuffix)

Description

Glue together all items into one String.

License

Open Source License

Parameter

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

Exception

Parameter Description
NullPointerException if <b>items</b> is null

Return

the resulting string

Declaration

public static String implodeArray(Object[] items, String prefix, String suffix, String delimiter,
        String lastItemSuffix) 

Method Source Code

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

public class Main {
    /**//from w  w  w . j  a va  2s .  c om
     * 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 implodeArray(Object[] 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.length > 0) {
            for (int i = 0; i < items.length - 1; ++i)
                sb.append(items[i]).append(delimiter);
            sb.append(items[items.length - 1]).append(lastItemSuffix);
        }

        return sb.append(suffix).toString();
    }
}

Related

  1. implode(String[] array, String separator)
  2. implode(String[] data, String glue)
  3. implode(String[] inputArray, String glueString)
  4. implode(String[] values, String separator, String beforeEach, String afterEach)
  5. implodeArray(Object[] array)
  6. implodeArray(String[] inputArray, String glueString)
  7. implodeArray(String[] inputArray, String glueString, int start)
  8. implodeString(String[] strArray, String strDelim)
  9. implodeStringArray(Object[] pieces, String delim)