Java Array Join join(String[] stringArray, String separator)

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

Description

Concatenates the Array of Strings into a String using the separator as the delimiter.

License

Open Source License

Parameter

Parameter Description
stringArray An Array of strings
separator The separating string

Return

A 'separator' separated String

Declaration

public static String join(String[] stringArray, String separator) 

Method Source Code


//package com.java2s;
/*// www .  j  a  va  2 s.com
 * Copyright (c) Thomas Parker, 2007, 2008.
 *  portions derived from CoreUtility.java
 *    Copyright 2002 (C) Bryan McRoberts <merton_monk@yahoo.com>
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * 
 */

import java.util.Collection;

public class Main {
    /**
     * Concatenates the Collection of Objects (converted to Strings using
     * .toString()) into a String using the separator as the delimiter.
     * 
     * This method is value-semantic and will not modify or maintain a reference
     * to the given Collection of Objects.
     * 
     * @param collection
     *            An Collection of objects
     * @param separator
     *            The separating string
     * @return A 'separator' separated String
     */
    public static String join(Collection<?> collection, String separator) {
        return joinToStringBuilder(collection, separator).toString();
    }

    /**
     * Concatenates the Array of Strings into a String using the separator as
     * the delimiter.
     * 
     * This method is value-semantic and will not modify or maintain a reference
     * to the given Array of strings.
     * 
     * @param stringArray
     *            An Array of strings
     * @param separator
     *            The separating string
     * @return A 'separator' separated String
     */
    public static String join(String[] stringArray, String separator) {
        if (stringArray == null) {
            return "";
        }

        StringBuilder result = new StringBuilder(stringArray.length * 10);

        boolean needjoin = false;

        for (String obj : stringArray) {
            if (needjoin) {
                result.append(separator);
            }
            needjoin = true;
            result.append(obj);
        }

        return result.toString();
    }

    /**
     * Concatenates the Collection of Objects (converted to Strings using
     * .toString()) into a StringBuilder using the separator as the delimiter.
     * 
     * This method is value-semantic and will not modify or maintain a reference
     * to the given Collection of Objects. Ownership of the returned
     * StringBuilder is transferred to the calling object. No reference to the
     * StringBuilder is maintained by StringUtil.
     * 
     * @param collection
     *            An Collection of objects
     * @param separator
     *            The separating character
     * @return A 'separator' separated StringBuilder
     */
    public static StringBuilder joinToStringBuilder(Collection<?> collection, String separator) {
        if (collection == null) {
            return new StringBuilder();
        }

        StringBuilder result = new StringBuilder(collection.size() * 10);

        boolean needjoin = false;

        for (Object obj : collection) {
            if (needjoin) {
                result.append(separator);
            }
            needjoin = true;
            result.append(obj.toString());
        }

        return result;
    }
}

Related

  1. join(String[] strArr)
  2. join(String[] strArray, String delimiter)
  3. join(String[] stringArray, boolean quote, String glue)
  4. join(String[] stringArray, String delimiter)
  5. join(String[] stringArray, String delimiterString, boolean joinNullValues)
  6. join(String[] strings)
  7. join(String[] strings, char delimiter)
  8. join(String[] strings, char delimiter)
  9. join(String[] strings, String glue)