Java String Join join(String separator, Collection strings)

Here you can find the source of join(String separator, Collection strings)

Description

Joins a number of strings together to form one long string, with the optional separator inserted between each of them.

License

Open Source License

Parameter

Parameter Description
separator a string to insert between each of the <code>strings</code> or <code>null</code>.
strings a Collection of strings.

Exception

Parameter Description
NullPointerException if <code>strings</code> is <code>null</code>.

Return

a string containing all of the strings joined together with separator between them.

Declaration

public static String join(String separator, Collection<String> strings) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;
import java.util.Collection;

public class Main {
    /**// w  ww . j  a v a  2 s.  co  m
     * Joins a number of <code>strings</code> together to form one long string,
     * with the optional <code>separator</code> inserted between each of them.
     * This method is implemented as convenience wrapper for the
     * {@link #join(String, Collection)} method.
     * 
     * @param separator a string to insert between each of the
     *          <code>strings</code> or <code>null</code>.
     * @param strings a variable list (an array) of strings.
     * @return a string containing all of the <code>strings</code> joined
     *         together with <code>separator</code> between them.
     * @see #join(String, Collection)
     */
    public static String join(String separator, String... strings) {
        return join(separator, Arrays.asList(strings));
    }

    /**
     * Joins a number of <code>strings</code> together to form one long string,
     * with the optional <code>separator</code> inserted between each of them.
     * 
     * @param separator a string to insert between each of the
     *          <code>strings</code> or <code>null</code>.
     * @param strings a {@link Collection} of strings.
     * @return a string containing all of the <code>strings</code> joined
     *         together with <code>separator</code> between them.
     * @throws NullPointerException if <code>strings</code> is <code>null</code>.
     */
    public static String join(String separator, Collection<String> strings) {
        StringBuilder builder = new StringBuilder();
        for (String string : strings) {
            if (builder.length() > 0 && separator != null)
                builder.append(separator);
            builder.append(string);
        }
        return builder.toString();
    }
}

Related

  1. join(String glue, String... str)
  2. join(String joiner, String... toJoin)
  3. join(String s, Object... objects)
  4. join(String sep, Object... pieces)
  5. join(String separator, CharSequence... elements)
  6. join(String separator, List objects)
  7. join(String separator, Object... objects)
  8. join(String separator, String... parts)
  9. join(String separator, String... strings)