Java Collection Join join(Collection s, String delimiter)

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

Description

Join a string array or list with a delimiter.

License

Apache License

Parameter

Parameter Description
s The array or list to join.
delimiter The delimiter to glue the pieces together with.

Return

The joined string.

Declaration

public static String join(Collection<?> s, String delimiter) 

Method Source Code


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

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**//from   w  w w. j  av  a  2 s .co m
     * Join a string array or list with a delimiter.
     *
     * @param s         The array or list to join.
     * @param delimiter The delimiter to glue the pieces together with.
     * @return The joined string.
     */
    public static String join(Collection<?> s, String delimiter) {
        StringBuilder builder = new StringBuilder();
        Iterator<?> iterator = s.iterator();
        while (iterator.hasNext()) {
            builder.append(iterator.next());
            if (!iterator.hasNext()) {
                break;
            }
            builder.append(delimiter);
        }
        return builder.toString();
    }
}

Related

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