Here you can find the source of implode(Collection
Parameter | Description |
---|---|
list | a collection of strings |
separator | the separater to be used |
public static String implode(Collection<String> list, String separator)
//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(); } }