Java Array Join join(String glue, String[] strings)

Here you can find the source of join(String glue, String[] strings)

Description

Takes a string 'glue' and array of strings, and returns a StringBuffer containing the strings joined with the glue between each of them.

License

Open Source License

Parameter

Parameter Description
glue The glue string
strings The strings to join

Return

a StringBuffer

Declaration

public static StringBuffer join(String glue, String[] strings) 

Method Source Code


//package com.java2s;

import java.util.Collection;

public class Main {
    /**//  w w w  . j a v  a2s .c  o  m
     * Takes a string 'glue' and array of strings, and returns a StringBuffer containing the strings joined with the glue
     * between each of them. The strings are joined in order   *
     *
     * @param glue    The glue string
     * @param strings The strings to join
     *
     * @return a StringBuffer
     */
    public static StringBuffer join(String glue, String[] strings) {
        return join(glue, strings, 0, strings.length - 1);
    }

    /**
     * Takes a string 'glue' and array of strings, and returns a StringBuffer containing the strings
     * between the specified indices (inclusive) joined with the glue between each of them. The strings are joined in order
     *
     * @param glue    The glue string
     * @param strings The strings to join
     * @param first   The index of the first string to join
     * @param last    The index of the last string to join
     *
     * @return a StringBuffer
     */
    public static StringBuffer join(String glue, String[] strings, int first, int last) {
        StringBuffer buf = new StringBuffer();
        for (int i = first; i <= last; i++) {
            String s = strings[i];
            if (i > first) {
                buf.append(glue);
            }
            buf.append(s);
        }
        return buf;
    }

    /**
     * Takes a string 'glue' and collection of CharSequences, and returns a StringBuffer containing the CharSequences
     * joined with the glue between each of them. They are joined in the order returned by the iterator of the colection
     *
     * @param glue          The glue string
     * @param charSequences The CharSequences to join
     *
     * @return a StringBuffer
     */
    public static <E> StringBuffer join(String glue, Collection<E> charSequences) {
        StringBuffer buf = new StringBuffer();
        int i = 0;
        for (Object charSequence : charSequences) {
            if (i > 0) {
                buf.append(glue);
            }
            buf.append(charSequence);
            i++;
        }
        return buf;
    }
}

Related

  1. join(String delimiter, short[] array)
  2. join(String delimiter, String[] array)
  3. join(String delimiter, String[] elements)
  4. join(String delimiter, String[] s)
  5. join(String glue, String[] strings)
  6. join(String s, String[] arr, int off, int end)
  7. join(String s[], String glue)
  8. join(String sep, final String[] strings)
  9. join(String sep, Object[] os)