Java String Array Combine combine(String[] values, String delimiter)

Here you can find the source of combine(String[] values, String delimiter)

Description

Combines the strings values in the string array into one single string, delimited by the specified delimiter.

License

Apache License

Parameter

Parameter Description
values The strings to be combined.
delimiter The delimiter used to separate the different strings.

Return

The resultant string combined from the string array separated by the specified delimiter. Return an emtpy String if the given values array is of size 0.

Declaration

public static String combine(String[] values, String delimiter) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from w  w  w  .  j a v a  2  s . c o m*/
     * Combines the strings values in the string array into one single string,
     * delimited by the specified delimiter. An emtpy String is returned if the
     * given values array is of size 0.
     * 
     * @param values The strings to be combined.
     * @param delimiter The delimiter used to separate the different strings.
     * @return The resultant string combined from the string array separated by
     *         the specified delimiter. Return an emtpy String if the given
     *         values array is of size 0.
     */
    public static String combine(String[] values, String delimiter) {

        if (values == null) {
            throw new NullPointerException("values array is null");
        }

        if (values.length == 0) {
            return "";
        }

        StringBuffer result = new StringBuffer();

        for (int i = 1; i < values.length; i++) {
            result.append(delimiter);
            result.append(values[i]);
        }

        result.insert(0, values[0]);

        return result.toString();
    }
}

Related

  1. combine(String... s)
  2. combine(String... vals)
  3. combine(String[] a, String[] b, String glue)
  4. combine(String[] array1, String[] array2)
  5. combine(String[] strs, String delimeter)
  6. combine(String[] values, String regex)
  7. combine(String[] values, String separator)
  8. combine(String[] words)
  9. combineArray(String separator, String... stringArray)