Java Collection Join join(Collection strings, String delimiter)

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

Description

Joins a Collection of String s in a single String using delimiter between each String .

License

LGPL

Parameter

Parameter Description
strings a parameter
delimiter a parameter

Return

a with all the s in strings concatenated.

Declaration

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

Method Source Code


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

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

public class Main {
    /**/*  w  ww.j  a  v a  2 s .c o m*/
     * Joins a {@link Collection} of {@link String}s in a single {@link String}
     * using <code>delimiter</code> between each {@link String}.
     * 
     * @param strings
     * @param delimiter
     * @return a {@link String} with all the {@link String}s in
     *         <code>strings</code> concatenated.
     */
    public static String join(Collection<String> strings, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator<String> iter = strings.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }

    /**
     * Joins a array of {@link String}s in a single {@link String} using
     * <code>delimiter</code> between each {@link String}.
     * 
     * @param strings
     * @param delimiter
     * @return a String
     */
    public static String join(String[] strings, String delimiter) {
        return join(Arrays.asList(strings), delimiter);
    }
}

Related

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