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

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

Description

Creates a String of all elements of an array, separated by a separator.

License

Open Source License

Parameter

Parameter Description
T the generic type
array the array
separator the separator

Return

the joined String

Declaration

public static <T> String join(T[] array, String separator) 

Method Source Code

//package com.java2s;
/**//from  w  ww.j  a v  a2s .c o  m
 * Copyright 2015 IBM Corp. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.util.Arrays;

public class Main {
    /**
     * Creates a String of all elements of an array, separated by a separator.
     *
     * @param <T> the generic type
     * @param array the array
     * @param separator the separator
     * @return the joined String
     */
    public static <T> String join(T[] array, String separator) {
        return join(Arrays.asList(array), separator);
    }

    /**
     * Creates a String of all elements of an iterable, separated by a separator.
     *
     * @param iterable the iterable
     * @param separator the separator
     * @return the joined String
     */
    public static String join(Iterable<?> iterable, String separator) {
        final StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (Object item : iterable) {
            if (first) {
                first = false;
            } else {
                sb.append(separator);
            }

            sb.append(item.toString());
        }

        return sb.toString();
    }
}

Related

  1. join(String[]... values)
  2. join(T[] a, T[] b)
  3. join(T[] arr1, T[] arr2)
  4. join(T[] array, String join)
  5. join(T[] array, String separator)
  6. join(T[] array, String separator)
  7. join(T[] array, String separator)
  8. join(T[] array, String separator)
  9. join(T[] array1, T[] array2)