Java Array Join join(final T[] array, final char c)

Here you can find the source of join(final T[] array, final char c)

Description

Join an array of objects with the given delimiter character.

License

Apache License

Parameter

Parameter Description
T a type
array the String array
c the delimiter character

Return

The joined String

Declaration

public static <T> String join(final T[] array, final char c) 

Method Source Code

//package com.java2s;
/**/*from w ww.  j  av a  2  s  . c  om*/
 * Copyright (c) 2008-2009 Acuity Technologies, Inc.
 *
 * 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.
 *
 * Created Jan 1, 2008
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Join a variable argument list using commas.
     *
     * @param args
     *        the objects
     * @return The joined String
     */
    public static String join(final Object... args) {
        return join(",", args);
    }

    /**
     * Join a variable argument list of objects with the given delimiter
     * character.
     *
     * @param args
     *        the objects
     * @param c
     *        the delimiter character
     * @return The joined String
     */
    public static String join(final char c, final Object... args) {
        return join(String.valueOf(c), args);
    }

    /**
     * Join a variable argument list of objects with the given delimiter String.
     *
     * @param args
     *        the objects
     * @param delimiter
     *        the delimiter String
     * @return The joined String
     */
    public static String join(final String delimiter, final Object... args) {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < args.length; ++i) {
            sb.append(args[i]);
            if (i + 1 < args.length) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }

    /**
     * Join an array of objects with the given delimiter character.
     *
     * @param <T>
     *        a type
     * @param array
     *        the String array
     * @param c
     *        the delimiter character
     * @return The joined String
     */
    public static <T> String join(final T[] array, final char c) {
        return join(array, String.valueOf(c));
    }

    /**
     * Join an array of objects with the given delimiter String.
     *
     * @param <T>
     *        a type
     * @param array
     *        the String array
     * @param delimiter
     *        the delimiter String
     * @return The joined String
     */
    public static <T> String join(final T[] array, final String delimiter) {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            sb.append(array[i]);
            if (i + 1 < array.length) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }

    /**
     * Joins a {@link Collection} of arbitrary objects with the given delimiter
     * character.
     *
     * @param <T>
     *        a type
     * @param collection
     *        the collection to join
     * @param c
     *        the delimiter character
     * @return The joined String
     */
    public static <T> String join(final Collection<T> collection, final char c) {
        return join(collection, String.valueOf(c));
    }

    /**
     * Joins a {@link Collection} of arbitrary objects with the given delimiter
     * String.
     *
     * @param <T>
     *        a type
     * @param collection
     *        the collection to join
     * @param delimiter
     *        the delimiter string
     * @return The joined String
     */
    public static <T> String join(final Collection<T> collection, final String delimiter) {
        final StringBuilder sb = new StringBuilder();
        for (final Iterator<T> i = collection.iterator(); i.hasNext();) {
            sb.append(i.next());
            if (i.hasNext()) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }
}

Related

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