Java Array Join join(CharSequence[] strings, CharSequence separator)

Here you can find the source of join(CharSequence[] strings, CharSequence separator)

Description

Joins the strings in 'strings' with 'separator' between each.

License

Open Source License

Declaration

public static String join(CharSequence[] strings, CharSequence separator) 

Method Source Code

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

import java.util.*;

public class Main {
    /**//w  w  w .j  a v a  2s.  c  o m
     * Joins the strings in 'strings' with 'separator' between each.
     */
    public static String join(CharSequence[] strings, CharSequence separator) {
        StringBuilder result = new StringBuilder();
        for (CharSequence string : strings) {
            if (result.length() > 0) {
                result.append(separator);
            }
            result.append(string);
        }
        return result.toString();
    }

    /**
     * Joins the strings in 'strings' with 'separator' between each.
     */
    public static String join(Iterable<?> strings, CharSequence separator) {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        Iterator<?> it = strings.iterator();
        while (it.hasNext()) {
            if (first) {
                first = false;
            } else {
                result.append(separator);
            }
            result.append(it.next());
        }
        return result.toString();
    }
}

Related

  1. arrayJoin(String[] array, char with)
  2. arrayJoinToString(Object[] array, CharSequence delim)
  3. join(byte[]... bs)
  4. join(char sep, Object[] array)
  5. join(CharSequence separator, String[] strings)
  6. join(final Object[] array)
  7. join(final Object[] array, final char separator)
  8. join(final Object[] array, final char separator)
  9. join(final String separator, final String[] array)