Java Array Join join(final String[] values, final String delimiter)

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

Description

joins list of strings using a delimiter.

License

Apache License

Parameter

Parameter Description
values - the string to concatenate
delimiter - delimiter to use between strings

Return

List of values delimeted by delimiter.

Declaration

public static String join(final String[] values, final String delimiter) 

Method Source Code


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

import java.util.List;

public class Main {
    public static final int AVERAGE_STRING_LENGTH = 20;

    /**// ww  w.  ja  v  a 2 s .c o  m
     * joins list of strings using a delimiter.
     *
     * @param values    - the string to concatenate
     * @param delimiter - delimiter to use between strings
     * @return List of values delimeted by delimiter.
     */
    public static String join(final String[] values, final String delimiter) {
        if (values == null) {
            return "";
        }
        final StringBuilder result = new StringBuilder(values.length * AVERAGE_STRING_LENGTH);
        for (int i = 0; i < values.length; i++) {
            result.append(values[i]);
            if (i < values.length - 1) {
                result.append(delimiter);
            }
        }
        return result.toString();
    }

    /**
     * joins list of strings using a delimiter.
     *
     * @param values    - the string to concatenate
     * @param delimiter - delimiter to use between strings
     * @return List of values delimited by delimiter.
     */
    public static String join(final List<String> values, final String delimiter) {
        if (values == null) {
            return "";
        }
        final StringBuilder result = new StringBuilder(values.size() * AVERAGE_STRING_LENGTH);
        for (int i = 0; i < values.size(); i++) {
            result.append(values.get(i));
            if (i < values.size() - 1) {
                result.append(delimiter);
            }
        }
        return result.toString();
    }
}

Related

  1. join(final String separator, final String[] array)
  2. join(final String[] arrays, final String seperator)
  3. join(final String[] in, final String sep)
  4. join(final String[] strings)
  5. join(final String[] strings, final String joinString)
  6. join(final T[] array, final char c)
  7. join(final T[] collection)
  8. join(int[] array, String separator)
  9. join(int[] follows)