Java Collection Join join(Collection s, String delimiter)

Here you can find the source of join(Collection s, String delimiter)

Description

Join a collection, akin to Perl's join(), using a given delimiter to produce a single String

License

Open Source License

Parameter

Parameter Description
s of type Collection
delimiter of type String

Exception

Parameter Description
IllegalArgumentException an exception

Return

String

Declaration

public static String join(Collection s, String delimiter)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*// w  w w . j  a v  a 2s .c om
 * Copyright (c) 2012. The Genome Analysis Centre, Norwich, UK
 * MISO project contacts: Robert Davey, Mario Caccamo @ TGAC
 * *********************************************************************
 *
 * This file is part of MISO.
 *
 * MISO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MISO 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MISO.  If not, see <http://www.gnu.org/licenses/>.
 *
 * *********************************************************************
 */

import java.util.*;

public class Main {
    /**
     * Join a collection, akin to Perl's join(), using a given delimiter to produce a single String 
     *
     * @param s of type Collection
     * @param delimiter of type String
     * @return String
     * @throws IllegalArgumentException
     */
    public static String join(Collection s, String delimiter)
            throws IllegalArgumentException {
        if (s == null) {
            throw new IllegalArgumentException(
                    "Collection to join must not be null");
        }
        StringBuffer buffer = new StringBuffer();
        Iterator iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext() && delimiter != null) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }

    /**
     * Join an Array, akin to Perl's join(), using a given delimiter to produce a single String
     *
     * @param s of type Object[]
     * @param delimiter of type String
     * @return String
     */
    public static String join(Object[] s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < s.length; i++) {
            buffer.append(s[i]);
            if (i < s.length - 1) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
}

Related

  1. join(Collection list, char separator)
  2. join(Collection lst, String delim)
  3. join(Collection objects, String glue)
  4. join(Collection paramCollection, String paramString)
  5. join(Collection parts, String sep)
  6. join(Collection s, String delimiter, boolean doQuote)
  7. join(Collection strings, String sep)
  8. join(Collection strings, String separator)
  9. join(Collection tokens, String separator)