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

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

Description

Glues together the array with delim inserted between elements.

License

GNU General Public License

Parameter

Parameter Description
ary The array to be glued together.
delim The delimiter to be put between elements.

Return

The glued array.

Declaration

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

Method Source Code

//package com.java2s;
/**//ww w . j  av a  2s .  c o m
 * 
 * @author Shinmera
 * @license GPLv3
 * @version 0.0.0
 */

import java.util.Map;

public class Main {
    /**
     * Glues together the array with delim inserted between elements.
     * 
     * @param ary The array to be glued together.
     * @param delim The delimiter to be put between elements.
     * @return The glued array.
     */
    public static String implode(Object[] ary, String delim) {
        return implode(ary, delim, 0, ary.length);
    }

    /**
     * Glues together the array from start out with delim inserted between each
     * element.
     * 
     * @param ary The array to be glued together.
     * @param delim The delimiter to be put between elements.
     * @return The glued array.
     */
    public static String implode(Object[] ary, String delim, int start) {
        return implode(ary, delim, start, ary.length);
    }

    /**
     * Glues together the array between start and stop with delim inserted
     * between each element.
     * 
     * @param ary The array to be glued together.
     * @param delim The delimiter to be put between elements.
     * @param start The start index.
     * @param stop The stop index.
     * @return The glued array.
     */
    public static String implode(Object[] ary, String delim, int start, int stop) {
        if (start < 0)
            throw new IllegalArgumentException("Start cannot be below zero.");
        if (stop < start)
            throw new IllegalArgumentException("Stop cannot be smaller than start.");
        if (stop > ary.length)
            throw new IllegalArgumentException("Stop cannot be larger than the array.");
        StringBuilder out = new StringBuilder();
        for (int i = start; i < stop; i++) {
            if (i != 0) {
                out.append(delim);
            }
            out.append(ary[i].toString());
        }
        return out.toString();
    }

    /**
     * Glues together the map with keyvaluedelim inserted between each key and
     * value and pairdelim between each key-value pair.
     * 
     * @param map The map to be glued together.
     * @param keyvaluedelim The delimiter to put between keys and values.
     * @param pairdelim The delimiter to put between pairs.
     * @return The glued map.
     */
    public static String implode(Map map, String keyvaluedelim, String pairdelim) {
        StringBuilder out = new StringBuilder();
        for (Object o : map.keySet()) {
            if (out.length() != 0)
                out.append(pairdelim);
            out.append(o).append(keyvaluedelim).append(map.get(o));
        }
        return out.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(String delim, Object[] objects)
  6. implode(String delim, String[] args)
  7. implode(String[] array, String separator)
  8. implode(String[] data, String glue)