Java List Implode implode(List list, String deliminator)

Here you can find the source of implode(List list, String deliminator)

Description

This is the inverse of 'explode'.

License

Open Source License

Declaration

public static String implode(List list, String deliminator) 

Method Source Code

//package com.java2s;
/**//from   ww  w.  j av  a 2 s .  c  om
 * com.mckoi.util.StringUtil  17 Dec 1999
 *
 * Mckoi SQL Database ( http://www.mckoi.com/database )
 * Copyright (C) 2000, 2001, 2002  Diehl and Associates, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * Version 2 as published by the Free Software Foundation.
 *
 * 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 Version 2 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * Version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Change Log:
 * 
 * 
 */

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

public class Main {
    /**
     * This is the inverse of 'explode'.  It forms a string by concatinating
     * each string in the list and seperating each with a deliminator string.
     * For example,
     * <code>
     *   implode(({"1", "150", "500"}), ",") = "1,150,500"
     * </code>
     */
    public static String implode(List list, String deliminator) {
        StringBuffer str = new StringBuffer();
        Iterator iter = list.iterator();
        boolean has_next = iter.hasNext();
        while (has_next) {
            str.append(iter.next().toString());
            has_next = iter.hasNext();
            if (has_next) {
                str.append(deliminator);
            }
        }
        return new String(str);
    }
}

Related

  1. implode(Collection list, String separator)
  2. implode(final List pieces, final String glue)
  3. implode(List objs, String delim)
  4. implode(List elements, String separator)
  5. implode(List list, String delimiter)
  6. implode(List list, String glue)