Java String Join join(String delimiter, Collection items)

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

Description

Join one or more items placing the specified delimiter between each item

License

Open Source License

Parameter

Parameter Description
delimiter The delimiter to place between each item. This value may be null, in which case, the empty string will be the delimiter
items The items to join. This value may be null, in which case, the empty string will be returned

Return

A single string that is the combination of the items and the delimiters

Declaration

public static String join(String delimiter, Collection<String> items) 

Method Source Code

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

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

public class Main {
    /**/*from   www  .ja v a2 s .  c  o m*/
     * The empty string
     */
    public static final String EMPTY = "";

    /**
     * Join one or more items placing the specified delimiter between each item
     * 
     * @param delimiter
     *            The delimiter to place between each item. This value may be
     *            null, in which case, the empty string will be the delimiter
     * @param items
     *            The items to join. This value may be null, in which case, the
     *            empty string will be returned
     * @return A single string that is the combination of the items and the
     *         delimiters
     */
    public static String join(String delimiter, Collection<String> items) {
        StringBuilder buffer = new StringBuilder();

        if (delimiter == null) {
            delimiter = "";
        }

        if (items != null) {
            for (String item : items) {
                if (item != null) {
                    buffer.append(item);
                }

                buffer.append(delimiter);
            }

            // trim off extra delimiter
            if (buffer.length() > 0) {
                buffer.setLength(buffer.length() - delimiter.length());
            }
        }

        return buffer.toString();
    }

    /**
     * Join one or more items placing the specified delimiter between each item
     * 
     * @param delimiter
     *            The delimiter to place between each item. This value may be
     *            null, in which case, the empty string will be the delimiter
     * @param items
     *            The items to join. This value may be null, in which case, the
     *            empty string will be returned
     * @return A single string that is the combination of the items and the
     *         delimiters
     */
    public static String join(String delimiter, String... items) {
        String result;

        if (items != null) {
            result = join(delimiter, Arrays.asList(items));
        } else {
            result = EMPTY;
        }

        return result;
    }
}

Related

  1. join(Map map, String separator)
  2. join(String connector, Object... objects)
  3. join(String delim, List strings)
  4. join(String delim, Object... objects)
  5. join(String delimiter, Collection objects)
  6. join(String delimiter, Iterable elements)
  7. join(String delimiter, Iterable strings)
  8. join(String delimiter, Iterable strings)
  9. join(String delimiter, Object... objects)