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.

No separator is added to the joined String.

License

Open Source License

Parameter

Parameter Description
array the array of values to join 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;
/*/*from w w  w . j av a2s .  c  o m*/
 * Copyright (c) 2007-2016 AREasy Runtime
 *
 * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software");
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT,
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 */

import java.util.Iterator;

public class Main {
    /**
     * The empty String <code>""</code>.
     */
    public static final String EMPTY = "";

    /**
     * <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>
     * StringUtility.join(null)            = null
     * StringUtility.join([])              = ""
     * StringUtility.join([null])          = ""
     * StringUtility.join(["a", "b", "c"]) = "abc"
     * StringUtility.join([null, "", "a"]) = "a"
     * </pre>
     *
     * @param array the array of values to join together, may be null
     * @return the joined String, <code>null</code> if null array input
     */
    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>
     * StringUtility.join(null, *)               = null
     * StringUtility.join([], *)                 = ""
     * StringUtility.join([null], *)             = ""
     * StringUtility.join(["a", "b", "c"], ';')  = "a;b;c"
     * StringUtility.join(["a", "b", "c"], null) = "abc"
     * StringUtility.join([null, "", "a"], ';')  = ";;a"
     * </pre>
     *
     * @param array    the array of values to join together, may be null
     * @param separator the separator character to use
     * @return the joined String, <code>null</code> if null array input
     */
    public static String join(Object[] array, char separator) {
        if (array == null)
            return null;

        int arraySize = array.length;
        int bufSize = (arraySize == 0 ? 0
                : ((array[0] == null ? 16 : array[0].toString().length()) + 1) * arraySize);
        StringBuffer buf = new StringBuffer(bufSize);

        for (int i = 0; i < arraySize; i++) {
            if (i > 0)
                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>
     * StringUtility.join(null, *)                = null
     * StringUtility.join([], *)                  = ""
     * StringUtility.join([null], *)              = ""
     * StringUtility.join(["a", "b", "c"], "--")  = "a--b--c"
     * StringUtility.join(["a", "b", "c"], null)  = "abc"
     * StringUtility.join(["a", "b", "c"], "")    = "abc"
     * StringUtility.join([null, "", "a"], ',')   = ",,a"
     * </pre>
     *
     * @param array    the array of values to join 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;

        if (separator == null)
            separator = EMPTY;
        int arraySize = array.length;

        // ArraySize ==  0: Len = 0
        // ArraySize > 0:   Len = NofStrings *(len(firstString) + len(separator))
        int bufSize = ((arraySize == 0) ? 0
                : arraySize * ((array[0] == null ? 16 : array[0].toString().length()) + separator.length()));

        StringBuffer buf = new StringBuffer(bufSize);

        for (int i = 0; i < arraySize; i++) {
            if (i > 0)
                buf.append(separator);
            if (array[i] != null)
                buf.append(array[i]);
        }

        return buf.toString();
    }

    /**
     * <p>Joins the elements of the provided <code>Iterator</code> into
     * a single String containing the provided elements.</p>
     * <p/>
     * <p>No delimiter is added before or after the list. Null objects or empty
     * strings within the iteration are represented by empty strings.</p>
     * <p/>
     * <p>See the examples here: {@link #join(Object[], char)}. </p>
     *
     * @param iterator  the <code>Iterator</code> of values to join together, may be null
     * @param separator the separator character to use
     * @return the joined String, <code>null</code> if null iterator input
     */
    public static String join(Iterator iterator, char separator) {
        if (iterator == null)
            return null;

        StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            if (obj != null)
                buf.append(obj);

            if (iterator.hasNext())
                buf.append(separator);
        }
        return buf.toString();
    }

    /**
     * <p>Joins the elements of the provided <code>Iterator</code> into
     * a single String containing the provided 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 ("").</p>
     * <p/>
     * <p>See the examples here: {@link #join(Object[], String)}. </p>
     *
     * @param iterator  the <code>Iterator</code> of values to join together, may be null
     * @param separator the separator character to use, null treated as ""
     * @return the joined String, <code>null</code> if null iterator input
     */
    public static String join(Iterator iterator, String separator) {
        if (iterator == null)
            return null;
        StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small

        while (iterator.hasNext()) {
            Object obj = iterator.next();
            if (obj != null)
                buf.append(obj);

            if ((separator != null) && iterator.hasNext())
                buf.append(separator);
        }
        return buf.toString();
    }

    /**
     * Append a new string arguments into an original text.
     */
    public static String append(String original, String argStr) {
        if (isNotEmpty(argStr))
            return (original + argStr);
        else
            return original;
    }

    /**
     * <p>Checks if a String is not empty ("") and not null.</p>
     * <p/>
     * <pre>
     * StringUtility.isNotEmpty(null)      = false
     * StringUtility.isNotEmpty("")        = false
     * StringUtility.isNotEmpty(" ")       = true
     * StringUtility.isNotEmpty("bob")     = true
     * StringUtility.isNotEmpty("  bob  ") = true
     * </pre>
     *
     * @param str the String to check, may be null
     * @return <code>true</code> if the String is not empty and not null
     */
    public static boolean isNotEmpty(String str) {
        return str != null && str.length() > 0;
    }
}

Related

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