Android Iterator Join join(Iterator strings, String sep)

Here you can find the source of join(Iterator strings, String sep)

Description

Join a collection of strings by a seperator

License

Open Source License

Parameter

Parameter Description
strings iterator of string objects
sep string to place between strings

Return

joined string

Declaration

public static String join(Iterator<String> strings, String sep) 

Method Source Code

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

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

public class Main {
    /***/*from  w w  w  . j a  va  2 s.com*/
     * Join a collection of strings by a seperator
     * @param strings collection of string objects
     * @param sep string to place between strings
     * @return joined string
     */
    public static String join(Collection<String> strings, String sep) {
        return join(strings.iterator(), sep);
    }

    /***
     * Join a collection of strings by a seperator
     * @param strings iterator of string objects
     * @param sep string to place between strings
     * @return joined string
     */
    public static String join(Iterator<String> strings, String sep) {
        if (!strings.hasNext())
            return "";

        String start = strings.next();
        if (!strings.hasNext()) // only one, avoid builder
            return start;

        StringBuilder sb = new StringBuilder(64).append(start);
        while (strings.hasNext()) {
            sb.append(sep);
            sb.append(strings.next());
        }
        return sb.toString();
    }
}

Related

  1. join(Iterator strings, String sep)
  2. join(Iterator strings, String sep)