Java Array Concatenate concatStrings(String[] strs, String delimiter)

Here you can find the source of concatStrings(String[] strs, String delimiter)

Description

Concatenates the given array of String s with the given delimiter.

License

Open Source License

Parameter

Parameter Description
strs the given array of String s.
delimiter the given delimiter.

Return

the concatenation.

Declaration

public static String concatStrings(String[] strs, String delimiter) 

Method Source Code


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

import java.util.Iterator;

public class Main {
    /**/*from ww w .  j  a v  a  2 s  .  c  om*/
     * Concatenates the given collection of {@code String}s with the given delimiter.
     *
     * @param strs the given collection of {@code String}s.
     * @param delimiter the given delimiter.
     * @return the concatenation.
     */
    public static String concatStrings(Iterable<String> strs, String delimiter) {
        StringBuilder builder = new StringBuilder();
        Iterator<String> iter = strs.iterator();

        while (iter.hasNext()) {
            builder.append(iter.next());
            if (iter.hasNext())
                builder.append(delimiter);
        }

        return builder.toString();
    }

    /**
     * Concatenates the given array of {@code String}s with the given delimiter.
     *
     * @param strs the given array of {@code String}s.
     * @param delimiter the given delimiter.
     * @return the concatenation.
     */
    public static String concatStrings(String[] strs, String delimiter) {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < strs.length; i++) {
            builder.append(strs[i]);
            if (i < strs.length - 1)
                builder.append(delimiter);
        }

        return builder.toString();
    }
}

Related

  1. concatenate2Arrays(T[] array1, T... array2)
  2. concatenateArray(Object[] srcOne, Object[] srcTwo)
  3. concatenateArrays(T[] first, T[]... rest)
  4. concatenateStrings(Object[] strings, String glueString)
  5. concatIntArrays(final int[] l1, final int[] l2)
  6. concatTwoStringArray(String[] first, String[] second)
  7. concatUniq(String[] part1, String[] part2)
  8. concatUnique(String[] a, String[] b)