Java Array Implode implode(String delim, Object[] objects)

Here you can find the source of implode(String delim, Object[] objects)

Description

This method merges an array of objects to a single string

License

Open Source License

Parameter

Parameter Description
delim the delimiter
objects the objects to implode

Return

the imploded string

Declaration

public static String implode(String delim, Object[] objects) 

Method Source Code

//package com.java2s;
/**//from   ww  w .  ja  v a 2  s .com
 * This file is part of CubeEngine.
 * CubeEngine is licensed under the GNU General Public License Version 3.
 *
 * CubeEngine is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CubeEngine 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CubeEngine.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;
import java.util.Iterator;

public class Main {
    /**
     * This method merges an array of objects to a single string
     *
     * @param delim   the delimiter
     * @param objects the objects to implode
     * @return the imploded string
     */
    public static String implode(String delim, Object[] objects) {
        return implode(delim, Arrays.asList(objects));
    }

    /**
     * This method merges an array of objects to a single string
     *
     * @param delimiter   the delimiter
     * @param objects the objects to implode
     * @return the imploded string
     */
    public static String implode(String delimiter, Iterable objects) {
        Iterator<?> iterator = objects.iterator();
        if (!iterator.hasNext()) {
            return "";
        }
        StringBuilder sb = new StringBuilder(iterator.next().toString());

        while (iterator.hasNext()) {
            sb.append(delimiter).append(iterator.next());
        }

        return sb.toString();
    }
}

Related

  1. implode(double[] array, String separator)
  2. implode(final String[] array, final String delim)
  3. implode(final String[] pStrArray)
  4. implode(Object[] array, String separator)
  5. implode(Object[] ary, String delim)
  6. implode(String delim, String[] args)
  7. implode(String[] array, String separator)
  8. implode(String[] data, String glue)
  9. implode(String[] inputArray, String glueString)