Here you can find the source of join(final Collection
Parameter | Description |
---|---|
aStrings | strings to join. |
aSeparator | a separator. |
public static String join(final Collection<String> aStrings, final String aSeparator)
//package com.java2s; /******************************************************************************* * Copyright (c) 2009-2010 Richard Eckart de Castilho. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors://from w ww . j av a2 s . co m * Richard Eckart de Castilho - initial API and implementation ******************************************************************************/ import java.util.Collection; import java.util.Iterator; public class Main { /** * Join the given strings into a single string separated by the given * separator. * * @param aStrings strings to join. * @param aSeparator a separator. * @return the joined string. */ public static String join(final String[] aStrings, final String aSeparator) { final StringBuilder sb = new StringBuilder(); for (int i = 0; i < aStrings.length; i++) { sb.append(aStrings[i]); if (i < aStrings.length - 1) { sb.append(aSeparator); } } return sb.toString(); } /** * Join the given strings into a single string separated by the given * separator. * * @param aStrings strings to join. * @param aSeparator a separator. * @return the joined string. */ public static String join(final Collection<String> aStrings, final String aSeparator) { final StringBuilder sb = new StringBuilder(); Iterator<String> i = aStrings.iterator(); while (i.hasNext()) { sb.append(i.next()); if (i.hasNext()) { sb.append(aSeparator); } } return sb.toString(); } }