Java Collection Join join(Collection strings, String delimiter)

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

Description

Join string pieces and separate with a delimiter.

License

Apache License

Parameter

Parameter Description
strings String pieces to join
delimiter Delimiter to put between string pieces

Return

One merged string

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**//from w  w w  . jav a2  s .c o  m
     * Join string pieces and separate with a delimiter.  Similar to the perl function of
     * the same name.  If strings or delimiter are null, null is returned.  Otherwise, at
     * least an empty string will be returned.    
     * @see #split
     *
     * @param strings String pieces to join
     * @param delimiter Delimiter to put between string pieces
     * @return One merged string
     */
    public static String join(Collection<String> strings, String delimiter) {
        if (strings == null || delimiter == null) {
            return null;
        }

        StringBuffer str = new StringBuffer();

        Iterator<String> iter = strings.iterator();
        while (iter.hasNext()) {
            str.append(iter.next());
            if (iter.hasNext()) {
                str.append(delimiter);
            }
        }

        return str.toString();
    }

    /**
     * Return a stringified version of the array.
     * @param array the array
     * @param delim the delimiter to use between array components
     * @return the string form of the array
     */
    public static String toString(final Object[] array, final String delim) {
        return toString(array, delim, true);
    }

    /**
     * Return a stringified version of the array.
     * @param array the array
     * @param delim the delimiter to use between array components
     * @return the string form of the array
     */
    public static String toString(final Object[] array, final String delim, boolean includeBrackets) {
        if (array == null) {
            return ""; //$NON-NLS-1$
        }
        final StringBuffer sb = new StringBuffer();
        if (includeBrackets) {
            sb.append('[');
        }
        for (int i = 0; i < array.length; ++i) {
            if (i != 0) {
                sb.append(delim);
            }
            sb.append(array[i]);
        }
        if (includeBrackets) {
            sb.append(']');
        }
        return sb.toString();
    }

    /**
     * Return a stringified version of the array, using a ',' as a delimiter
     * @param array the array
     * @return the string form of the array
     * @see #toString(Object[], String)
     */
    public static String toString(final Object[] array) {
        return toString(array, ",", true); //$NON-NLS-1$
    }
}

Related

  1. join(Collection strings)
  2. join(Collection strings, String delimiter)
  3. join(Collection strings, String delimiter)
  4. join(Collection strings, String delimiter)
  5. join(Collection strings, String delimiter)
  6. join(Collection strings, String sep)
  7. join(Collection strings, String sep)
  8. join(Collection strings, String separator)
  9. join(Collection strings, String separator)