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

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

Description

Joins the items in array with a delimiter.

License

Apache License

Parameter

Parameter Description
array array of items to join.
delimiter delimiter to insert between elements of array.

Return

string with array elements separated by delimiter. There is no trailing delimiter in the string.

Declaration

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

Method Source Code

//package com.java2s;
/*/* w ww . ja v  a2  s .c om*/
Copyright 2009-2016 Igor Polevoy
    
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.*;

public class Main {
    /**
     * Joins the items in array with a delimiter.
     *
     * @param array array of items to join.
     * @param delimiter delimiter to insert between elements of array.
     * @return string with array elements separated by delimiter. There is no trailing delimiter in the string.
     */
    public static String join(String[] array, String delimiter) {
        if (empty(array)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        join(sb, array, delimiter);
        return sb.toString();
    }

    /**
     * Joins the items in collection with a delimiter.
     *
     * @param collection collection of items to join.
     * @param delimiter delimiter to insert between elements of collection.
     * @return string with collection elements separated by delimiter. There is no trailing delimiter in the string.
     */
    public static String join(Collection<?> collection, String delimiter) {
        if (collection.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        join(sb, collection, delimiter);
        return sb.toString();
    }

    /**
     * Joins the items in collection with a delimiter, and appends the result to StringBuilder.
     *
     * @param sb StringBuilder to append result to
     * @param collection collection of items to join.
     * @param delimiter delimiter to insert between elements of collection.
     */
    public static void join(StringBuilder sb, Collection<?> collection, String delimiter) {
        if (collection.isEmpty()) {
            return;
        }
        Iterator<?> it = collection.iterator();
        sb.append(it.next());
        while (it.hasNext()) {
            sb.append(delimiter);
            sb.append(it.next());
        }
    }

    /**
     * Joins the items in array with a delimiter, and appends the result to StringBuilder.
     *
     * @param sb StringBuilder to append result to
     * @param array array of items to join.
     * @param delimiter delimiter to insert between elements of array.
     */
    public static void join(StringBuilder sb, Object[] array, String delimiter) {
        if (empty(array)) {
            return;
        }
        sb.append(array[0]);
        for (int i = 1; i < array.length; i++) {
            sb.append(delimiter);
            sb.append(array[i]);
        }
    }

    /**
     * Returns true if array is either null or empty.
     *
     * @param array array to check
     * @return true if array is either null or empty, false otherwise
     */
    public static boolean empty(Object[] array) {
        return array == null || array.length == 0;
    }

    /**
     * Returns true if collection is either null or empty.
     *
     * @param collection collection to check
     * @return true if collection is either null or empty, false otherwise
     */
    public static boolean empty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }
}

Related

  1. join(String[] array)
  2. join(String[] array)
  3. join(String[] array, String delim)
  4. join(String[] array, String delimiter)
  5. join(String[] array, String delimiter)
  6. join(String[] array, String sep)
  7. join(String[] array, String separator)
  8. join(String[] array, String separator)
  9. join(String[] array, String separator)