Java String Implode implode(Object strarr[], String delim)

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

Description

Same as implode(Iterable strarr, String delim), but taking an array instead of an Iterable object.

License

Open Source License

Parameter

Parameter Description
strarr a parameter
delim a parameter

Declaration

public static final String implode(Object strarr[], String delim) 

Method Source Code

//package com.java2s;
//    it under the terms of the GNU General Public License as published by

public class Main {
    /**//w w  w  . j  a  v a 2  s.  co  m
     * Mimicking the php implode function. Takes any iterable object, and
     * implodes its components using their .toString() method, putting the
     * delimiter delim between each of them.
     * 
     * @param strarr
     * @param delim
     * @return
     */
    @SuppressWarnings("unchecked")
    public static final String implode(Iterable strarr, String delim) {
        String res = "";
        String del = "";
        int i = 0;
        for (Object c : strarr) {
            i++;
            if (i > 1)
                del = delim;
            res += (del + c.toString());
        }
        return res;
    }

    /**
     * Same as implode(Iterable strarr, String delim), but taking an array
     * instead of an Iterable object.
     * 
     * @param strarr
     * @param delim
     * @return
     */
    public static final String implode(Object strarr[], String delim) {
        String res = "";
        String del = "";
        int i = 0;
        for (Object c : strarr) {
            i++;
            if (i > 1)
                del = delim;
            res += (del + c.toString());
        }
        return res;
    }
}

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[] data, String delimiter)
  5. implode(Object[] elements, String delimiter)
  6. implode(Object[] source, String delimiter)
  7. implode(String delim, Object[] array)