Java List Join join(final List toJoin)

Here you can find the source of join(final List toJoin)

Description

Joins strings from list of strings, using comma as separator to one string.

License

Open Source License

Parameter

Parameter Description
toJoin list of strings to join

Declaration

public static String join(final List toJoin) 

Method Source Code

//package com.java2s;
/*//from   www.j  a v a  2 s. co  m
 * Copyright 2001-2008 Aqris Software AS. All rights reserved.
 * 
 * This program is dual-licensed under both the Common Development
 * and Distribution License ("CDDL") and the GNU General Public
 * License ("GPL"). You may elect to use one or the other of these
 * licenses.
 */

import java.util.List;

public class Main {
    /**
     * Joins strings from list of strings, using comma as separator to one string.
     * @param toJoin list of strings to join
     */
    public static String join(final List toJoin) {
        return join(toJoin, ",");
    }

    /**
     * Joins strings from list of strings, using custom separator.
     * @param toJoin list of strings to join
     * @param joiner string to use as separator
     */
    public static String join(final List toJoin, final String joiner) {
        final String[] tmp = (String[]) toJoin.toArray(new String[toJoin.size()]);
        return join(tmp, joiner);
    }

    /**
     * Joins strings from array of strings, using comma as separator to one string.
     * @param toJoin array of strings to join
     */
    public static String join(final String[] toJoin) {
        return join(toJoin, ",");
    }

    /**
     * Joins strings from array of strings, using custom separator.
     * @param toJoin array of strings to join
     * @param joiner string to use as separator
     */
    public static String join(final String[] toJoin, final String joiner) {
        if (toJoin.length == 0) {
            return "";
        }
        String retVal = toJoin[0];
        for (int q = 1; q < toJoin.length; q++) {
            retVal += joiner + toJoin[q];
        }
        return retVal;
    }
}

Related

  1. join(Collection list, String separator)
  2. join(Collection stringList, String seperator)
  3. join(Collection list, String delimiter)
  4. join(Collection list, String seperator)
  5. join(final List list, final String conjunction)
  6. join(final List list, final CharSequence delim)
  7. join(final List args)
  8. join(final List list, final String separator)
  9. join(final List things, final String on)