Java List Join join(List strings, String delimiter)

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

Description

Join string pieces and separate with a delimiter.

License

Open Source 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(List strings, String delimiter) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w.  j av  a 2s  . c  o  m*/
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * 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(List strings, String delimiter) {
        if (strings == null || delimiter == null) {
            return null;
        }

        StringBuffer str = new StringBuffer();

        // This is the standard problem of not putting a delimiter after the last
        // string piece but still handling the special cases. A typical way is to check every
        // iteration if it is the last one and skip the delimiter - this is avoided by
        // looping up to the last one, then appending just the last one.

        // First we loop through all but the last one (if there are at least 2) and
        // put the piece and a delimiter after it. An iterator is used to walk the list.
        int most = strings.size() - 1;
        if (strings.size() > 1) {
            Iterator iter = strings.iterator();
            for (int i = 0; i < most; i++) {
                str.append(iter.next());
                str.append(delimiter);
            }
        }

        // If there is at least one element, put the last one on with no delimiter after.
        if (strings.size() > 0) {
            str.append(strings.get(most));
        }

        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) {
        if (array == null) {
            return ""; //$NON-NLS-1$
        }
        if (array.length == 0) {
            return "[]"; //$NON-NLS-1$
        }
        final StringBuffer sb = new StringBuffer();
        sb.append('[');
        for (int i = 0; i < array.length; ++i) {
            if (i != 0) {
                sb.append(delim);
            }
            sb.append(array[i]);
        }
        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, ","); //$NON-NLS-1$
    }
}

Related

  1. join(List list, String flag)
  2. join(List list, String separator)
  3. join(List list, String separator)
  4. join(List objects)
  5. join(List objects, String joiner)
  6. join(List vec, String separator)
  7. join(List items, CharSequence delimiter)
  8. join(List s, String delimiter)
  9. join(List s, String delimiter)