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

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

Description

Combine the string elements in the array to form a new string, composed of all the elements where each element is separated by the delimiter string.

License

Open Source License

Parameter

Parameter Description
array The array to join
delimiter The delimiter between array components

Return

null if the array is empty, the string itself it the array contains only one element, or the combined string with delimiters otherwise

Declaration

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

Method Source Code

//package com.java2s;
/*//  w w  w .  j  a v a2 s .c o  m
  Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
  This file is part of the Semantic Discovery Toolkit.
    
  The Semantic Discovery Toolkit 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 3 of the License, or
  (at your option) any later version.
    
  The Semantic Discovery Toolkit 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 The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
  */

import java.util.Collection;

public class Main {
    /**
       * Combine the string elements in the array to form a new string, composed of all the elements
       * where each element is separated by the delimiter string.  This method is the opposite of the 
       * String.split(String delimiter) command.  Null elements are not added to the string, nor do they appear delimited.  Empty strings, however
       * do appear between  two delimiters.
       * @param array          The array to join
       * @param delimiter      The delimiter between array components
       *
       * @return null if the array is empty, the string itself it the array contains only one element, or the combined string with delimiters otherwise
       */
    public static final String join(String[] array, String delimiter) {
        String result = null;

        if (delimiter == null) {
            delimiter = "";
        }

        if (array != null) {
            if (array.length == 0) {
                return result;
            }
            result = array[0];

            for (int i = 1; i < array.length; i++) {
                if (array[i] != null) {
                    result = result + delimiter + array[i];
                }
            }
        }

        return result;
    }

    /**
       * Alternate format for the join method that accepts any collection<String> in place of a String[] array.
       *
       * @param delimiter   The delimeter between collection components
       * @param collection  The collection to join
       */
    public static final String join(Collection<String> collection, String delimiter) {
        return join(collection.toArray(new String[collection.size()]), delimiter);
    }
}

Related

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