Java String Join join(Iterable arr, String delimeter)

Here you can find the source of join(Iterable arr, String delimeter)

Description

Concatenate a list of values together as a string.

License

Open Source License

Parameter

Parameter Description
arr The array to convert to a string.
delimeter A string that will separate each element in `arr'.

Return

All elements of `arr' delimited by `delimiter'.

Declaration

public static String join(Iterable<? extends Object> arr, String delimeter) 

Method Source Code


//package com.java2s;
/*// w w w. j  a va2 s  .  co m
 * This file is part of the Raster Storage Archive (RSA).
 *
 * The RSA 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.
 *
 * The RSA 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
 * the RSA.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2013 CRCSI - Cooperative Research Centre for Spatial Information
 * http://www.crcsi.com.au/
 */

import java.util.Arrays;

public class Main {
    /**
     * Concatenate a list of values together as a string.
     * 
     * @param arr The array to convert to a string.
     * @param delimeter A string that will separate each element in `arr'.
     * @return All elements of `arr' delimited by `delimiter'.
     */
    public static String join(Iterable<? extends Object> arr, String delimeter) {
        StringBuilder str = new StringBuilder();

        boolean first = true;
        for (Object ob : arr) {
            if (first) {
                first = false;
            } else {
                str.append(delimeter);
            }
            str.append(ob);
        }

        return str.toString();
    }

    /**
     * Concatenate a list of values together as a string using the specified
     * delimiter.
     * 
     * @param arr The array to convert to a string.
     * @param delimeter A string that will separate each element in `arr'.
     * @return All elements of `arr' delimited by `delimiter'.
     */
    public static String join(Object[] arr, String delimeter) {
        return join(Arrays.asList(arr), delimeter);
    }

    /**
     * Concatenate a list of values together as a string.
     * 
     * @param arr The array to convert to a string.
     * @return All elements of `arr' delimited by a comma and space (", ").
     */
    public static String join(Iterable<? extends Object> arr) {
        return join(arr, ", ");
    }

    /**
     * Concatenate a list of values together as a string using the comma
     * delimiter.
     * 
     * @param arr The array to convert to a string.
     * @return All elements of `arr' delimited by a comma and space (", ").
     */
    public static String join(Object[] arr) {
        return join(Arrays.asList(arr), ", ");
    }
}

Related

  1. join(final String sep, final Iterable strs)
  2. join(final String separator, final Collection objs)
  3. join(final String separator, final String... strings)
  4. join(final String... arguments)
  5. join(HashMap params, String separator)
  6. join(Iterable input)
  7. join(java.util.Collection strings, String delimiter)
  8. join(Map map, String separator)
  9. join(String connector, Object... objects)