Java Array Join join(Object[] array, String delimiter)

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

Description

Joins elements of the defined array to a single string.

License

Open Source License

Parameter

Parameter Description
array the values to join, may be null
delimiter the delimiter used between array elements

Return

the joined String, null if null input

Declaration

public static String join(Object[] array, String delimiter) 

Method Source Code


//package com.java2s;
/*/*from w ww . j  av  a 2  s.c  o  m*/
Copyright (c) 2012 Thomas Zink, 
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Iterator;

public class Main {
    /**
      * Joins elements of the defined array to a single string. This is the
      * reverse to {@code String.split()}
      * 
      * Nulls or empty strings ("") will become "".
      *
      * @param array  the values to join, may be null
      * @param delimiter  the delimiter used between array elements
      * @return the joined String, {@code null} if null input
      */
    public static String join(Object[] array, String delimiter) {
        if (array == null) {
            return null;
        }

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

    /**
     * Joins elements of the defined array to a single string. This is the
     * reverse to {@code String.split()}
     * 
     * Nulls or empty strings ("") will become "".
     *
     * @param array  the values to join, may be null
     * @param delimiter  the delimiter used between array elements
     * @param start  index to start joining from
     * @param stop  index to stop joining (exclusive)
     * @return the joined String, {@code null} if null input
     */
    public static String join(Object[] array, String separator, int start, int stop) {
        if (array == null) {
            return null;
        }
        int num;
        if (((num = (stop - start)) <= 0) || (start < 0) || (stop > array.length)) {
            return "";
        }

        StringBuilder buf = new StringBuilder(num + num * separator.length());

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

        return buf.toString();
    }

    /**
     * Joins elements of the defined {@code Iterable} to a single string.
     * This is the reverse to {@code String.split()}.
     *
     * Nulls or empty strings ("") will become "".
     * 
     * @param iterable  the {@code Iterable} to join, may be null
     * @param delimiter  the delimiter used between array elements
     * @return the joined String, {@code null} if null input
     */
    public static String join(Iterable<?> iterable, String separator) {
        if (iterable == null) {
            return null;
        }
        return join(iterable.iterator(), separator);
    }

    /**
      * Joins elements of the defined {@code Iterable} to a single string.
      * This is the reverse to {@code String.split()}.
      *
      * Nulls or empty strings ("") will become "".
      * 
      * @param iterator  the {@code Iterator} to join, may be null
      * @param delimiter  the delimiter used between array elements
      * @return the joined String, {@code null} if null input
      */
    public static String join(Iterator<?> iterator, String separator) {
        if (iterator == null) {
            return null;
        }
        if (!iterator.hasNext()) {
            return "";
        }

        StringBuilder buf = new StringBuilder(128);

        Object next = iterator.next();
        if (next != null) {
            buf.append(next);
        }

        while (iterator.hasNext()) {
            next = iterator.next();
            if (separator != null) {
                buf.append(separator);
            }
            if (next != null) {
                buf.append(next);
            }
        }

        return buf.toString();
    }
}

Related

  1. join(Object[] array, char separator)
  2. join(Object[] array, char separator)
  3. join(Object[] array, char separator, int startIndex, int endIndex)
  4. join(Object[] array, String delim)
  5. join(Object[] array, String delimiter)
  6. join(Object[] array, String glue)
  7. join(Object[] array, String linker)
  8. join(Object[] array, String sep)
  9. join(Object[] array, String separator)