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

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

Description

Joins the elements of the given array to a string, separated by the given separator string.

License

Apache License

Parameter

Parameter Description
array the array to join
separator the separator string

Return

a string made up of the string representations of the given array's members, separated by the given separator string

Declaration

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

Method Source Code


//package com.java2s;
/*/*ww w  .  j  a  v  a 2s. c o  m*/
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
 */

import java.util.Arrays;

public class Main {
    /**
     * Joins the elements of the given array to a string, separated by the given separator string.
     *
     * @param array the array to join
     * @param separator the separator string
     *
     * @return a string made up of the string representations of the given array's members, separated by the given separator
     *         string
     */
    public static String join(Object[] array, String separator) {
        return array != null ? join(Arrays.asList(array), separator) : null;
    }

    /**
     * Joins the elements of the given iterable to a string, separated by the given separator string.
     *
     * @param iterable the iterable to join
     * @param separator the separator string
     *
     * @return a string made up of the string representations of the given iterable members, separated by the given separator
     *         string
     */
    public static String join(Iterable<?> iterable, String separator) {
        if (iterable == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;

        for (Object object : iterable) {
            if (!isFirst) {
                sb.append(separator);
            } else {
                isFirst = false;
            }

            sb.append(object);
        }

        return sb.toString();
    }
}

Related

  1. join(Object[] array, String separator)
  2. join(Object[] array, String separator)
  3. join(Object[] array, String separator)
  4. join(Object[] array, String separator)
  5. join(Object[] array, String separator)
  6. join(Object[] array, String separator)
  7. join(Object[] array, String seperator)
  8. join(Object[] elements, CharSequence separator)
  9. join(Object[] elements, String glue)