Java Array Join join(final String separator, final String[] array)

Here you can find the source of join(final String separator, final String[] array)

Description

join

License

Open Source License

Parameter

Parameter Description
separator the separator to join array of string
array the array of strings to join

Return

the joined string

Declaration

public static String join(final String separator, final String[] array) 

Method Source Code


//package com.java2s;
import java.util.Collection;

public class Main {
    /**/*  w w  w. j a v  a 2 s . c  om*/
     * @param separator the separator to join array of string
     * @param array the array of strings to join
     * @return the joined string
     */
    public static String join(final String separator, final String[] array) {

        String result = null;
        for (String str : array) {
            result = (result == null ? "" : result + separator) + str;
        }
        return result;
    }

    /**
     * @param separator the separator to join array of string
     * @param before the string will insert before every item in array
     * @param after the string will append after every item in array
     * @param array the array of strings to join
     * @return the joined string
     */
    public static String join(final String separator, final String before, final String after,
            final String[] array) {

        if ((array != null) && (array.length > 0)) {
            String result = null;
            for (String str : array) {
                result = (result == null ? "" : result + separator) + before + str + after;
            }
            return result;
        } else {
            return "";
        }
    }

    /**
     * @param separator the separator to join array of string
     * @param before the string will insert before every item in array
     * @param after the string will append after every item in array
     * @param collection collection of strings to join
     * @return the joined string
     */
    public static String join(final String separator, final String before, final String after,
            final Collection<String> collection) {

        if ((collection != null) && (collection.size() > 0)) {
            String result = null;
            for (String str : collection) {
                result = (result == null ? "" : result + separator) + before + str + after;
            }
            return result;
        } else {
            return "";
        }
    }
}

Related

  1. join(CharSequence separator, String[] strings)
  2. join(CharSequence[] strings, CharSequence separator)
  3. join(final Object[] array)
  4. join(final Object[] array, final char separator)
  5. join(final Object[] array, final char separator)
  6. join(final String[] arrays, final String seperator)
  7. join(final String[] in, final String sep)
  8. join(final String[] strings)
  9. join(final String[] strings, final String joinString)