Java String Join join(java.util.Collection strings, String delimiter)

Here you can find the source of join(java.util.Collection strings, String delimiter)

Description

Returns a string representation of 'strings' which contains all elements of 'strings' separated by 'delimiter'.

License

Open Source License

Declaration

public static String join(java.util.Collection<?> strings, String delimiter) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2006-2013 AITIA International, Inc.
 * // w  w  w . j a  v a 2 s  . co m
 * This program 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.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

public class Main {
    /** Returns a string representation of 'strings' which contains all elements
     *  of 'strings' separated by 'delimiter'.
     */
    public static String join(java.util.Collection<?> strings, String delimiter) {
        StringBuilder ans = new StringBuilder();
        boolean first = true;
        for (Object s : strings) {
            if (first)
                first = false;
            else
                ans.append(delimiter);
            ans.append(s);
        }
        return ans.toString();
    }

    /** Returns a string representation of 'args' which contains all elements
     *  of 'args' separated by 'delimiter'.
     */
    public static String join(String delimiter, Object... args) {
        if (args.length == 1) {
            if (args[0] instanceof Object[])
                args = (Object[]) args[0];
            else if (args[0] instanceof java.util.Collection)
                return join((java.util.Collection<?>) args[0], delimiter);
        }
        return join(java.util.Arrays.asList(args), delimiter);
    }

    /** Appends 'args' to 'sb'. 
     * @return sb
     */
    public static StringBuilder append(StringBuilder sb, Object... args) {
        if (sb == null)
            sb = new StringBuilder();
        for (int i = 0; i < args.length; ++i)
            sb.append(args[i]);
        return sb;
    }
}

Related

  1. join(final String separator, final String... strings)
  2. join(final String... arguments)
  3. join(HashMap params, String separator)
  4. join(Iterable arr, String delimeter)
  5. join(Iterable input)
  6. join(Map map, String separator)
  7. join(String connector, Object... objects)
  8. join(String delim, List strings)
  9. join(String delim, Object... objects)