Java String Array Combine combineSplit(final int startIndex, final String[] string, final String seperator)

Here you can find the source of combineSplit(final int startIndex, final String[] string, final String seperator)

Description

Turns a String[] into a single String separated by the passed separator.

License

Apache License

Parameter

Parameter Description
startIndex The index to start combining from.
string The array to combine from.
seperator The String to append between each String in the string array.

Return

A containing all items from a string array with the provided separator between them.

Declaration

public static String combineSplit(final int startIndex, final String[] string, final String seperator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2017 jamietech//  ww  w .ja v a 2s  . com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/

public class Main {
    /**
     * Turns a String[] into a single String separated by the passed separator.
     * @param startIndex The index to start combining from.
     * @param string The array to combine from.
     * @param seperator The String to append between each String in the string array.
     * @return A {@link String} containing all items from a string array with the provided separator between them.
     */
    public static String combineSplit(final int startIndex, final String[] string, final String seperator) {
        if (startIndex + 1 > string.length) {
            return "";
        }

        final StringBuilder builder = new StringBuilder();

        for (int i = startIndex; i < string.length; i++) {
            builder.append(string[i]);
            builder.append(seperator);
        }

        builder.deleteCharAt(builder.length() - seperator.length());
        return builder.toString();
    }
}

Related

  1. combineArray(String[] a, String[] b)
  2. combineArray(String[] args)
  3. combineArray(String[] array, int offset)
  4. combineArray(String[] str, int start, int end)
  5. combineArrays(String[] array1, String[] array2)
  6. combineSplit(int startIndex, String[] string, String separator)
  7. combineSplit(int startIndex, String[] string, String separator)
  8. combineSplit(int startIndex, String[] string, String separator)
  9. combineString(String[] strArr, String glue)