Java String Array Combine combineArray(String separator, String... stringArray)

Here you can find the source of combineArray(String separator, String... stringArray)

Description

Combines a set of strings into a single string, separated by the given character set

License

Open Source License

Parameter

Parameter Description
separator character set included between each part of the given array
stringArray array to combine

Return

the combined string

Declaration

public static String combineArray(String separator, String... stringArray) 

Method Source Code

//package com.java2s;
/*/*ww w. ja v  a2s  .  c  o m*/
 * This file is part of Commodus.
 *
 * Commodus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Commodus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Commodus.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Combines a set of strings into a single string, separated by the given character set
     *
     * @param separator   character set included between each part of the given array
     * @param stringArray array to combine
     * @return the combined string
     */
    public static String combineArray(String separator, String... stringArray) {
        return combineArray(0, separator, stringArray);
    }

    /**
     * Combines a set of strings into a single string, separated by the given character set
     *
     * @param startIndex  index to begin the separation at, inclusive
     * @param separator   character set included between each part of the given array
     * @param stringArray array to combine
     * @return the combined string
     */
    public static String combineArray(int startIndex, String separator, String... stringArray) {
        return combineArray(startIndex, stringArray.length, separator, stringArray);
    }

    /**
     * Combines a set of strings into a single string, separated by the given character set
     *
     * @param startIndex  index to begin the separation at, inclusive
     * @param endIndex    index to end the separation at, exclusive
     * @param separator   character set included between each part of the given array
     * @param stringArray array to combine
     * @return the combined string
     */
    public static String combineArray(int startIndex, int endIndex, String separator, String... stringArray) {
        if (stringArray == null || startIndex >= endIndex) {
            return "";
        } else {
            StringBuilder builder = new StringBuilder();
            for (int i = startIndex; i < endIndex; i++) {
                builder.append(stringArray[i]);
                builder.append(separator);
            }
            builder.delete(builder.length() - separator.length(), builder.length());
            return builder.toString();
        }
    }
}

Related

  1. combine(String[] strs, String delimeter)
  2. combine(String[] values, String delimiter)
  3. combine(String[] values, String regex)
  4. combine(String[] values, String separator)
  5. combine(String[] words)
  6. combineArray(String[] a, String[] b)
  7. combineArray(String[] args)
  8. combineArray(String[] array, int offset)
  9. combineArray(String[] str, int start, int end)