Java Array Join join(T[] array, String separator)

Here you can find the source of join(T[] array, String separator)

Description

A helper method to form a string using the values inside the array.

License

Open Source License

Parameter

Parameter Description
array The array of values.
separator The separator to be used.
T The type of the values inside the collection.

Return

The formatted string.

Declaration

public static <T> String join(T[] array, String separator) 

Method Source Code

//package com.java2s;
/**/*from w ww .  j av a  2s  .  c  o m*/
 * Utilities.java
 * ---------------------------------
 * Copyright (c) 2016
 * RESOLVE Software Research Group
 * School of Computing
 * Clemson University
 * All rights reserved.
 * ---------------------------------
 * This file is subject to the terms and conditions defined in
 * file 'LICENSE.txt', which is part of this source code package.
 */

import java.util.*;

public class Main {
    /**
     * <p>A helper method to form a string using the values inside the
     * Java collection.</p>
     *
     * @param data The collection of values.
     * @param separator The separator to be used.
     * @param <T> The type of the values inside the collection.
     *
     * @return The formatted string.
     */
    public static <T> String join(Collection<T> data, String separator) {
        return join(data, separator, "", "");
    }

    /**
     * <p>A helper method to form a string using the values inside the
     * Java collection.</p>
     *
     * @param data The collection of values.
     * @param separator The separator to be used.
     * @param left The left most value for the string.
     * @param right The right most value for the string.
     * @param <T> The type of the values inside the collection.
     *
     * @return The formatted string.
     */
    public static <T> String join(Collection<T> data, String separator, String left, String right) {
        return join(data.iterator(), separator, left, right);
    }

    /**
     * <p>A helper method to form a string using an iterator.</p>
     *
     * @param iter An iterator for the collection of values.
     * @param separator The separator to be used.
     * @param left The left most value for the string.
     * @param right The right most value for the string.
     * @param <T> The type of the values inside the collection.
     *
     * @return The formatted string.
     */
    public static <T> String join(Iterator<T> iter, String separator, String left, String right) {
        StringBuilder buf = new StringBuilder();

        buf.append(left);
        while (iter.hasNext()) {
            buf.append(iter.next());
            if (iter.hasNext()) {
                buf.append(separator);
            }
        }
        buf.append(right);

        return buf.toString();
    }

    /**
     * <p>A helper method to form a string using the values inside the
     * array.</p>
     *
     * @param array The array of values.
     * @param separator The separator to be used.
     * @param <T> The type of the values inside the collection.
     *
     * @return The formatted string.
     */
    public static <T> String join(T[] array, String separator) {
        return join(Arrays.asList(array), separator);
    }
}

Related

  1. join(T[] array, String join)
  2. join(T[] array, String separator)
  3. join(T[] array, String separator)
  4. join(T[] array, String separator)
  5. join(T[] array, String separator)
  6. join(T[] array1, T[] array2)
  7. join(T[] first, T[] second)
  8. join(T[] objs, String splitString)
  9. joinArrays(T[] a, T[] b)