Java String Implode implode(final char separator, final Object... array)

Here you can find the source of implode(final char separator, final Object... array)

Description

Join array elements with a string.

License

Apache License

Parameter

Parameter Description
separator The special glue string
array The array of strings to implode.

Return

Return the joined String.

Declaration

public static String implode(final char separator, final Object... array) 

Method Source Code

//package com.java2s;
/*//from w  ww.  j a v a 2s . co m
 *     Copyright 2016-2026 TinyZ
 *
 * 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 {
    /**
     * Join array elements with a string.
     * <pre>
     *     implode(new String[]{"a", "b", "c"}, '-') => "a-b-c"
     * </pre>
     * @param separator The special glue string
     * @param array     The array of strings to implode.
     * @return Return the joined String.
     */
    public static String implode(final char separator, final Object... array) {
        return implode(array, separator);
    }

    /**
     * Join array elements with a string.
     * <pre>
     *     implode(new String[]{"a", "b", "c"}, '-') => "a-b-c"
     * </pre>
     * @param array     The array of Object to implode.
     * @param separator The special glue string
     * @return Return the joined String.
     */
    public static String implode(final Object[] array, final char separator) {
        if (array == null || array.length <= 0) {
            return null;
        }
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                sb.append(separator);
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
}

Related

  1. implode(char delimiter, char escape, String... input)
  2. implode(final String separator, final Iterable data)
  3. implode(Object strarr[], String delim)
  4. implode(Object[] data, String delimiter)
  5. implode(Object[] elements, String delimiter)