Java Array Implode implodeStringArray(Object[] pieces, String delim)

Here you can find the source of implodeStringArray(Object[] pieces, String delim)

Description

Implode an object array into a single string, delimited by a given delimiter

License

Open Source License

Parameter

Parameter Description
pieces - an array of objects
delim - a delimiter

Return

concatenated representation of the string array

Declaration

public static String implodeStringArray(Object[] pieces, String delim) 

Method Source Code

//package com.java2s;
/**/*from  w w w  .  j a  v a  2s .co  m*/
 *
 *  BibSonomy-Common - Common things (e.g., exceptions, enums, utils, etc.)
 *
 *  Copyright (C) 2006 - 2011 Knowledge & Data Engineering Group,
 *                            University of Kassel, Germany
 *                            http://www.kde.cs.uni-kassel.de/
 *
 *  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
 *  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 program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.util.Arrays;
import java.util.Collection;

public class Main {
    /**
     * Implode an object array into a single string, delimited by a given delimiter
     * 
     * @param pieces -
     *          an array of objects
     * @param delim -
     *          a delimiter
     * @return concatenated representation of the string array
     */
    public static String implodeStringArray(Object[] pieces, String delim) {
        return implodeStringCollection(Arrays.asList(pieces), delim);
    }

    /**
     * Implode a collection of objects into a single String, delimited by a given delimiter.
     * 
     * @param stringList - a list of objects s1, s2, ... - the objects toString() 
     *          method is called to create the list
     * @param delim - a delimiter _d_
     * @return a concatenated representation of the string collection s1_d_s2_d_...
     */
    public static String implodeStringCollection(final Collection<? extends Object> stringList,
            final String delim) {
        if (stringList == null || delim == null)
            return "";
        int i = 0;
        final StringBuilder sb = new StringBuilder();
        for (final Object elem : stringList) {
            if (i != 0) {
                sb.append(delim);
            }
            sb.append(elem);
            i++;
        }
        return sb.toString();
    }
}

Related

  1. implodeArray(Object[] array)
  2. implodeArray(Object[] items, String prefix, String suffix, String delimiter, String lastItemSuffix)
  3. implodeArray(String[] inputArray, String glueString)
  4. implodeArray(String[] inputArray, String glueString, int start)
  5. implodeString(String[] strArray, String strDelim)