Here you can find the source of join(T[] array, String separator)
A helper method to form a string using the values inside the array.
Parameter | Description |
---|---|
array | The array of values. |
separator | The separator to be used. |
T | The type of the values inside the collection. |
public static <T> String join(T[] array, String separator)
//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); } }