Java Array Join join(Object[] array, String delim)

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

Description

join the given string array with the given delimiter

License

Open Source License

Declaration

public static String join(Object[] array, String delim) 

Method Source Code

//package com.java2s;
/***/*from  www  . j  a v a2s . com*/
 **
 ** This library 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 library 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 **
 **/

import java.util.*;

public class Main {
    /**
     * join the given string array with the given delimiter
     */
    public static String join(Iterator it, String delim) {
        StringBuilder joined = new StringBuilder();
        if (it != null) {
            while (it.hasNext()) {
                joined.append(it.next());
                if (it.hasNext()) {
                    joined.append(delim);
                }
            }
        }
        return joined.toString();
    }

    /**
     * join the given string array with the given delimiter
     */
    public static String join(Object[] array, String delim) {
        return join(array, 0, array.length, delim);
    }

    /**
     * join the given string array with the given delimiter
     */
    public static String join(Object[] array, int start, int end, String delim) {
        if (array != null) {
            if (start > array.length || end < start) {
                return null;
            }
            if (start < 0) {
                start = 0;
            }
            if (end > array.length) {
                end = array.length;
            }
            StringBuilder joined = new StringBuilder();
            for (int i = start; i < end; i++) {
                joined.append(array[i]);
                if ((i + 1) < end) {
                    joined.append(delim);
                }
            }
            return joined.toString();
        } else {
            return null;
        }
    }

    /**
     * join the given string array with the given delimiter
     */
    public static String join(Iterable iterable, String delim) {
        return iterable != null ? join(iterable.iterator(), delim) : null;
    }
}

Related

  1. join(Object[] array, char separator)
  2. join(Object[] array, char separator)
  3. join(Object[] array, char separator)
  4. join(Object[] array, char separator)
  5. join(Object[] array, char separator, int startIndex, int endIndex)
  6. join(Object[] array, String delimiter)
  7. join(Object[] array, String delimiter)
  8. join(Object[] array, String glue)
  9. join(Object[] array, String linker)