Java Array Join join(Object[] array)

Here you can find the source of join(Object[] array)

Description

Joins the elements of the provided array into a single String containing the provided list of elements.

License

Open Source License

Parameter

Parameter Description
array the array of values to joinBy together, may be null

Return

the joined String, null if null array input

Declaration

public static String join(Object[] array) 

Method Source Code

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

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

import java.util.Iterator;

import java.util.List;

public class Main {
    public static final String EMPTY = "";

    /**/*from ww  w.ja v a 2 s  . co m*/
     * Joins together some number of elements to form a comma separated list.
     */
    public static String join(List<?> elements) {
        return join(elements, ", ");
    }

    /**
     * Joins together some number of elements. If a symbol in the list is the
     * empty string, it is replaced with the string "(blank)".
     *
     * @param elements
     *            objects to be joined together
     * @param sep
     *            used between elements when joining
     */
    public static String join(List<?> elements, String sep) {
        switch (elements.size()) {
        case 0:
            return "";

        case 1:
            return elements.get(0).toString();

        default:

            StringBuilder buffer = new StringBuilder();
            boolean first = true;

            for (Object o : elements) {
                if (!first) {
                    buffer.append(sep);
                }
                String str = String.valueOf(o);
                if (str.length() == 0) {
                    str = "(blank)";
                }
                buffer.append(str);
                first = false;
            }

            return buffer.toString();
        }
    }

    /**
     * <p>
     * Joins the elements of the provided array into a single String containing
     * the provided list of elements.
     * </p>
     * <p/>
     * <p>
     * No separator is added to the joined String. Null objects or empty strings
     * within the array are represented by empty strings.
     * </p>
     * <p/>
     *
     * <pre>
     * StringUtils.joinBy(null)            = null
     * StringUtils.joinBy([])              = ""
     * StringUtils.joinBy([null])          = ""
     * StringUtils.joinBy(["a", "b", "c"]) = "abc"
     * StringUtils.joinBy([null, "", "a"]) = "a"
     * </pre>
     *
     * @param array
     *            the array of values to joinBy together, may be null
     * @return the joined String, <code>null</code> if null array input
     * @since 2.0
     */
    public static String join(Object[] array) {
        return join(array, null);
    }

    /**
     * <p>
     * Joins the elements of the provided array into a single String containing
     * the provided list of elements.
     * </p>
     * <p/>
     * <p>
     * No delimiter is added before or after the list. Null objects or empty
     * strings within the array are represented by empty strings.
     * </p>
     * <p/>
     *
     * <pre>
     * StringUtils.joinBy(null, *)               = null
     * StringUtils.joinBy([], *)                 = ""
     * StringUtils.joinBy([null], *)             = ""
     * StringUtils.joinBy(["a", "b", "c"], ';')  = "a;b;c"
     * StringUtils.joinBy(["a", "b", "c"], null) = "abc"
     * StringUtils.joinBy([null, "", "a"], ';')  = ";;a"
     * </pre>
     *
     * @param array
     *            the array of values to joinBy together, may be null
     * @param separator
     *            the separator character to use
     * @return the joined String, <code>null</code> if null array input
     * @since 2.0
     */
    public static String join(Object[] array, char separator) {
        if (array == null) {
            return null;
        }

        return join(array, separator, 0, array.length);
    }

    /**
     * <p>
     * Joins the elements of the provided array into a single String containing
     * the provided list of elements.
     * </p>
     * <p/>
     * <p>
     * No delimiter is added before or after the list. Null objects or empty
     * strings within the array are represented by empty strings.
     * </p>
     * <p/>
     *
     * <pre>
     * StringUtils.joinBy(null, *)               = null
     * StringUtils.joinBy([], *)                 = ""
     * StringUtils.joinBy([null], *)             = ""
     * StringUtils.joinBy(["a", "b", "c"], ';')  = "a;b;c"
     * StringUtils.joinBy(["a", "b", "c"], null) = "abc"
     * StringUtils.joinBy([null, "", "a"], ';')  = ";;a"
     * </pre>
     *
     * @param array
     *            the array of values to joinBy together, may be null
     * @param separator
     *            the separator character to use
     * @param startIndex
     *            the first index to start joining from. It is an error to pass
     *            in an end index past the end of the array
     * @param endIndex
     *            the index to stop joining from (exclusive). It is an error to
     *            pass in an end index past the end of the array
     * @return the joined String, <code>null</code> if null array input
     * @since 2.0
     */
    public static String join(Object[] array, char separator, int startIndex, int endIndex) {
        if (array == null) {
            return null;
        }
        int bufSize = endIndex - startIndex;
        if (bufSize <= 0) {
            return EMPTY;
        }

        bufSize *= (array[startIndex] == null ? 16 : array[startIndex].toString().length()) + 1;
        StringBuilder buf = new StringBuilder(bufSize);

        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

    /**
     * <p>
     * Joins the elements of the provided array into a single String containing
     * the provided list of elements.
     * </p>
     * <p/>
     * <p>
     * No delimiter is added before or after the list. A <code>null</code>
     * separator is the same as an empty String (""). Null objects or empty
     * strings within the array are represented by empty strings.
     * </p>
     * <p/>
     *
     * <pre>
     * StringUtils.joinBy(null, *)                = null
     * StringUtils.joinBy([], *)                  = ""
     * StringUtils.joinBy([null], *)              = ""
     * StringUtils.joinBy(["a", "b", "c"], "--")  = "a--b--c"
     * StringUtils.joinBy(["a", "b", "c"], null)  = "abc"
     * StringUtils.joinBy(["a", "b", "c"], "")    = "abc"
     * StringUtils.joinBy([null, "", "a"], ',')   = ",,a"
     * </pre>
     *
     * @param array
     *            the array of values to joinBy together, may be null
     * @param separator
     *            the separator character to use, null treated as ""
     * @return the joined String, <code>null</code> if null array input
     */
    public static String join(Object[] array, String separator) {
        if (array == null) {
            return null;
        }
        return join(array, separator, 0, array.length);
    }

    /**
     * <p>
     * Joins the elements of the provided array into a single String containing
     * the provided list of elements.
     * </p>
     * <p/>
     * <p>
     * No delimiter is added before or after the list. A <code>null</code>
     * separator is the same as an empty String (""). Null objects or empty
     * strings within the array are represented by empty strings.
     * </p>
     * <p/>
     *
     * <pre>
     * StringUtils.joinBy(null, *)                = null
     * StringUtils.joinBy([], *)                  = ""
     * StringUtils.joinBy([null], *)              = ""
     * StringUtils.joinBy(["a", "b", "c"], "--")  = "a--b--c"
     * StringUtils.joinBy(["a", "b", "c"], null)  = "abc"
     * StringUtils.joinBy(["a", "b", "c"], "")    = "abc"
     * StringUtils.joinBy([null, "", "a"], ',')   = ",,a"
     * </pre>
     *
     * @param array
     *            the array of values to joinBy together, may be null
     * @param separator
     *            the separator character to use, null treated as ""
     * @param startIndex
     *            the first index to start joining from. It is an error to pass
     *            in an end index past the end of the array
     * @param endIndex
     *            the index to stop joining from (exclusive). It is an error to
     *            pass in an end index past the end of the array
     * @return the joined String, <code>null</code> if null array input
     */
    public static String join(Object[] array, String separator, int startIndex, int endIndex) {
        if (array == null) {
            return null;
        }
        if (separator == null) {
            separator = EMPTY;
        }

        // endIndex - startIndex > 0: Len = NofStrings *(len(firstString) +
        // len(separator))
        // (Assuming that all Strings are roughly equally long)
        int bufSize = endIndex - startIndex;
        if (bufSize <= 0) {
            return EMPTY;
        }

        bufSize *= (array[startIndex] == null ? 16 : array[startIndex].toString().length()) + separator.length();

        StringBuilder buf = new StringBuilder(bufSize);

        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

    /**
     * Join a Collection of Strings together.
     *
     * @param glue
     *            Token to place between Strings.
     * @param pieces
     *            Collection of Strings to joinBy.
     * @return String presentation of joined Strings.
     * @see #join(String, java.util.Iterator)
     */
    public final static String join(String glue, Collection<String> pieces) {
        return join(glue, pieces.iterator());
    }

    /**
     * Join an Iteration of Strings together.
     * <p/>
     * <h5>Example</h5>
     * <p/>
     *
     * <pre>
     * // getValue Iterator of Strings (&quot;abc&quot;,&quot;def&quot;,&quot;123&quot;);
     * Iterator i = getIterator();
     * out.print(TextUtils.joinBy(&quot;, &quot;, i));
     * // prints: &quot;abc, def, 123&quot;
     * </pre>
     *
     * @param glue
     *            Token to place between Strings.
     * @param pieces
     *            Iteration of Strings to joinBy.
     * @return String presentation of joined Strings.
     */
    public final static String join(String glue, Iterator<String> pieces) {
        StringBuilder sb = new StringBuilder();
        while (pieces.hasNext()) {
            sb.append(pieces.next());
            if (pieces.hasNext()) {
                sb.append(glue);
            }
        }
        return sb.toString();
    }

    /**
     * Join an array of Strings together.
     *
     * @param glue
     *            Token to place between Strings.
     * @param pieces
     *            ArrayEx of Strings to joinBy.
     * @return String presentation of joined Strings.
     * @see #join(String, java.util.Iterator)
     */
    public final static String join(String glue, String[] pieces) {
        return join(glue, Arrays.asList(pieces).iterator());
    }

    /**
     * Gets a String's length or <code>0</code> if the String is
     * <code>null</code>.
     *
     * @param str
     *            a String or <code>null</code>
     * @return String length or <code>0</code> if the String is
     *         <code>null</code>.
     * @since 2.4
     */
    public static int length(String str) {
        return str == null ? 0 : str.length();
    }
}

Related

  1. join(Object[] arr)
  2. join(Object[] arr, String separator)
  3. join(Object[] arr, String separator)
  4. join(Object[] array)
  5. join(Object[] array)
  6. join(Object[] array)
  7. join(Object[] array)
  8. join(Object[] array, char separator)
  9. join(Object[] array, char separator)