Java List Implode implode(Collection list, String separator)

Here you can find the source of implode(Collection list, String separator)

Description

Implodes a collection of strings.

License

Open Source License

Parameter

Parameter Description
list a collection of strings
separator the separater to be used

Return

a single string containing all elements separated by the separator or an empty string if the collection was null or empty.

Declaration

public static String implode(Collection<String> list, String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Gijs de Vries aka Janoz.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /* ww  w.jav a  2 s .co m*/
 * Contributors:
 *     Gijs de Vries aka Janoz - initial API and implementation
 ******************************************************************************/

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**
     * Implodes a collection of strings.
     * @param list a collection of strings
     * @param separator the separater to be used
     * @return a single string containing all elements separated by 
     *       the separator or an empty string if the collection was 
     *       null or empty.
     */
    public static String implode(Collection<String> list, String separator) {
        if (list == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Iterator<String> i = list.iterator();
        while (i.hasNext()) {
            sb.append(i.next());
            if (i.hasNext()) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }
}

Related

  1. implode(final List pieces, final String glue)
  2. implode(List list, String deliminator)
  3. implode(List objs, String delim)
  4. implode(List elements, String separator)