Java Iterable Join join(String delimiter, Iterable strings)

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

Description

Join multiple strings with delimiter.

License

Open Source License

Parameter

Parameter Description
delimiter A String that is used as glue between the elements of strings .
strings An Iterable of String s that are to be concatenated.

Return

A with the concatenated elements of strings .

Declaration

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

Method Source Code

//package com.java2s;
/*//from w w  w.j  a v  a 2s.  c  o m
 * dmfs - http://dmfs.org/
 *
 * Copyright (C) 2011 Marten Gajda <marten@dmfs.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

import java.util.Iterator;

public class Main {
    public final static String EMPTY_STRING = "";

    /**
     * Join multiple strings with delimiter.
     * 
     * @param delimiter
     *            A {@link String} that is used as glue between the elements of {@code strings}.
     * 
     * @param strings
     *            Array of {@link String}s that are to be concatenated.
     * 
     * @return A {@link String} with the concatenated elements of {@code strings}.
     */
    public static String join(String delimiter, String... strings) {
        if (strings == null) {
            return null;
        }
        int count;
        if ((count = strings.length) == 0) {
            // no need to instantiate a StringBuilder
            return EMPTY_STRING;
        }

        int len = 0;
        for (String s : strings) {
            len += s.length();
        }

        StringBuilder sb = new StringBuilder((count - 1) * delimiter.length() + len);

        sb.append(strings[0]);
        for (int i = 1; i < count; ++i) {
            sb.append(delimiter);
            sb.append(strings[i]);
        }
        return sb.toString();
    }

    /**
     * Join multiple strings with delimiter.
     * 
     * @param delimiter
     *            A {@link String} that is used as glue between the elements of {@code strings}.
     * 
     * @param strings
     *            An {@link Iterable} of {@link String}s that are to be concatenated.
     * 
     * @return A {@link String} with the concatenated elements of {@code strings}.
     */
    public static String join(String delimiter, Iterable<String> strings) {
        if (strings == null) {
            return null;
        }

        int len = 0;
        int count = 0;
        for (String s : strings) {
            len += s.length();
            ++count;
        }

        if (count == 0) {
            // no need to instantiate a StringBuilder
            return EMPTY_STRING;
        }

        StringBuilder sb = new StringBuilder((count - 1) * delimiter.length() + len);

        Iterator<String> it = strings.iterator();
        sb.append(it.next());
        while (it.hasNext()) {
            sb.append(delimiter);
            sb.append(it.next());
        }
        return sb.toString();
    }

    /**
     * Join multiple strings with delimiter.
     * 
     * @param delimiter
     *            A {@code char} that is used as glue between the elements of {@code strings}.
     * 
     * @param strings
     *            Array of {@link String}s that are to be concatenated.
     * 
     * @return A {@link String} with the concatenated elements of {@code strings}.
     */
    public static String join(char delimiter, String... strings) {
        if (strings == null) {
            return null;
        }
        int count;
        if ((count = strings.length) == 0) {
            // no need to instantiate a StringBuilder
            return EMPTY_STRING;
        }

        int len = 0;
        for (String s : strings) {
            len += s.length();
        }

        StringBuilder sb = new StringBuilder(strings.length - 1 + len);

        sb.append(strings[0]);
        for (int i = 1; i < count; ++i) {
            sb.append(delimiter);
            sb.append(strings[i]);
        }
        return sb.toString();
    }

    /**
     * Join multiple strings with delimiter.
     * 
     * @param delimiter
     *            A {@code char} that is used as glue between the elements of {@code strings}.
     * 
     * @param strings
     *            An {@link Iterable} of {@link String}s that are to be concatenated.
     * 
     * @return A {@link String} with the concatenated elements of {@code strings}.
     */
    public static String join(char delimiter, Iterable<String> strings) {
        if (strings == null) {
            return null;
        }

        int len = 0;
        int count = 0;
        for (String s : strings) {
            len += s.length();
            ++count;
        }

        if (count == 0) {
            // no need to instantiate a StringBuilder
            return EMPTY_STRING;
        }

        StringBuilder sb = new StringBuilder(count - 1 + len);

        Iterator<String> it = strings.iterator();
        sb.append(it.next());
        while (it.hasNext()) {
            sb.append(delimiter);
            sb.append(it.next());
        }
        return sb.toString();
    }

    /**
     * Join multiple strings with delimiter.
     * 
     * @param strings
     *            An {@link Iterable} of {@link String}s that are to be concatenated.
     * 
     * @return A {@link String} with the concatenated elements of {@code strings}.
     */
    public static String join(Iterable<String> strings) {
        if (strings == null) {
            return null;
        }

        int len = 0;
        for (String s : strings) {
            len += s.length();
        }

        if (len == 0) {
            // no need to instantiate a StringBuilder
            return EMPTY_STRING;
        }

        StringBuilder sb = new StringBuilder(len);

        for (String s : strings) {
            sb.append(s);
        }
        return sb.toString();
    }
}

Related

  1. join(Iterable parts, String delimiter)
  2. join(Iterable sequence, String delimiter)
  3. join(Iterable values, String separator)
  4. join(Iterable values, String separator)
  5. join(Iterator iterator, String separator)
  6. join(String delimiter, Iterable stringsIterable)
  7. join(String delimiter, String wrap, Iterable objs)
  8. join(String glue, Iterable pieces)
  9. join(String glue, Iterable tokens)