Java Collection Join join(final Collection aStrings, final String aSeparator)

Here you can find the source of join(final Collection aStrings, final String aSeparator)

Description

Join the given strings into a single string separated by the given separator.

License

Open Source License

Parameter

Parameter Description
aStrings strings to join.
aSeparator a separator.

Return

the joined string.

Declaration

public static String join(final Collection<String> aStrings, final String aSeparator) 

Method Source Code

//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();
    }
}

Related

  1. join(final Collection coll, final String glue)
  2. join(final Collection items, final String inBetween)
  3. join(final Collection list, final String inbetween)
  4. join(final Collection strings, String delimeter)
  5. join(final Collection values, final String separator)
  6. join(final Collection chunks, final String delimiter)
  7. join(final Collection collection, final String separator)
  8. join(final Collection list, final String separator)
  9. join(final Collection seq, final String delimiter)

  10. HOME | Copyright © www.java2s.com 2016