Java List Implode implodeList(final List list, final String glue)

Here you can find the source of implodeList(final List list, final String glue)

Description

Implode a list of values (Like php's implode)

License

Open Source License

Parameter

Parameter Description
list The list to implode
glue The glue to use between values

Return

The imploded string

Declaration

public static String implodeList(final List<?> list, final String glue) 

Method Source Code

//package com.java2s;
/**// www .  java 2s .c om
 * Sleeksnap, the open source cross-platform screenshot uploader
 * Copyright (C) 2012 Nikki <nikki@nikkii.us>
 *
 * This program 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.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Implode a list of values (Like php's implode)
     * 
     * @param list
     *            The list to implode
     * @param glue
     *            The glue to use between values
     * @return The imploded string
     */
    public static String implodeList(final List<?> list, final String glue) {
        final StringBuilder builder = new StringBuilder();
        for (final Iterator<?> it = list.iterator(); it.hasNext();) {
            builder.append(it.next());
            if (it.hasNext()) {
                builder.append(glue);
            }
        }
        return builder.toString();
    }
}

Related

  1. implode(List list, String glue)
  2. implode(List strings, String separator)
  3. implode(List tokens, char separator, char escapeCharacter)
  4. implode(String glue, List list)
  5. implode(String split, List list)