Java List Implode implode(final List pieces, final String glue)

Here you can find the source of implode(final List pieces, final String glue)

Description

Joins a list of strings by using delimiter as glue

License

Open Source License

Parameter

Parameter Description
pieces The different pieces to be joined by glue
glue The glue used to join the different pieces

Return

A string containing a a string representation of all elements in pieces in their respective orders, with a glue string between each of them (but not before the first or after the last one).

Declaration

public static String implode(final List<String> pieces, final String glue) 

Method Source Code

//package com.java2s;
/*// w  w  w. j  ava  2s . c  om
 * Copyright (C) 2011  Ives van der Flaas
 *
 * 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.List;

public class Main {
    /**
     * Joins a list of strings by using delimiter as glue
     * 
     * @param pieces
     *            The different pieces to be joined by glue
     * @param glue
     *            The glue used to join the different pieces
     * @return A string containing a a string representation of all elements in
     *         pieces in their respective orders, with a glue string between
     *         each of them (but not before the first or after the last one).
     */
    public static String implode(final List<String> pieces, final String glue) {
        String out = "";
        for (int i = 0; i < pieces.size(); i++) {
            if (i != 0) {
                out += glue;
            }
            out += pieces.get(i);
        }
        return out;
    }
}

Related

  1. implode(Collection list, String separator)
  2. implode(List list, String deliminator)
  3. implode(List objs, String delim)
  4. implode(List elements, String separator)
  5. implode(List list, String delimiter)