Java String Implode implode(Object[] elements, String delimiter)

Here you can find the source of implode(Object[] elements, String delimiter)

Description

Combines an array to a string, using the specified delimiter.

License

Open Source License

Parameter

Parameter Description
elements the array to combine to a single string.
delimiter the delimiter to put between the combined elements.

Return

the array combined to a string.

Declaration

public static String implode(Object[] elements, String delimiter) 

Method Source Code

//package com.java2s;
/**//w ww.  j  a  v a  2s. co m
 *  NetP5 is a processing and java library for tcp and udp ip communication.
 *
 *  2006 by Andreas Schlegel
 *
 * 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., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 * @author Andreas Schlegel (http://www.sojamo.de)
 *
 */

public class Main {
    /**
     * Combines an array to a string, using the specified delimiter.
     *
     * @param elements
     *            the array to combine to a single string.
     * @param delimiter
     *            the delimiter to put between the combined elements.
     * @return the array combined to a string.
     */
    public static String implode(Object[] elements, String delimiter) {
        StringBuffer buffer = new StringBuffer("");
        for (int i = 0; i < elements.length - 1; i++) {
            buffer.append((String) elements[i] + delimiter);
        }
        buffer.append((String) elements[elements.length - 1]);
        return buffer.toString();
    }

    /**
     * Combines an array to a string, using a comma and a space as delimiter.
     *
     * @param elements
     *            the array to combine to a single string.
     * @return the array combined to a string.
     */
    public static String implode(Object[] elements) {
        return implode(elements, ", ");
    }
}

Related

  1. implode(char delimiter, char escape, String... input)
  2. implode(final char separator, final Object... array)
  3. implode(final String separator, final Iterable data)
  4. implode(Object strarr[], String delim)
  5. implode(Object[] data, String delimiter)
  6. implode(Object[] source, String delimiter)
  7. implode(String delim, Object[] array)
  8. implode(String delimiter, String[] strings)
  9. implode(String glue, String... parts)