Here you can find the source of join(String separator, List> elements)
Parameter | Description |
---|---|
separator | Separator string. |
elements | List of elements that should be concatenated. |
public static String join(String separator, List<?> elements)
//package com.java2s; /* ****************************************************************************** * Copyright (c) 2006-2013 XMind Ltd. and others. * * This file is a part of XMind 3. XMind releases 3 and * above are dual-licensed under the Eclipse Public License (EPL), * which is available at http://www.eclipse.org/legal/epl-v10.html * and the GNU Lesser General Public License (LGPL), * which is available at http://www.gnu.org/licenses/lgpl.html * See http://www.xmind.net/license.html for details. * * Contributors://w w w. j ava 2s . c o m * XMind Ltd. - initial API and implementation *******************************************************************************/ import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { /** * Returns a string containing all elements concatenated by a specified * separator. * * @param separator * Separator string. * @param elements * List of elements that should be concatenated. * @return a concatenated string. */ public static String join(String separator, List<?> elements) { if (elements == null || elements.size() == 0) { return ""; //$NON-NLS-1$ } StringBuffer sb = new StringBuffer(elements.size() * 3); int i = 0; for (Object elem : elements) { if (i++ > 0) { sb.append(separator); } sb.append(format(elem)); } return sb.toString(); } /** * Returns a string containing all elements concatenated by a specified * separator. * * @param separator * Separator string. * @param elements * List of elements that should be concatenated. * @return a concatenated string. */ public static String join(String separator, Object... elements) { if (elements == null || elements.length == 0) { return ""; //$NON-NLS-1$ } return join(separator, Arrays.asList(elements)); } /** * Returns a string with all float values concatenated by a specified * separator. * * @param separator * Separator string. * @param elements * Float array. * @return a concatenated string. */ public static String join(String separator, float... elements) { if (elements == null || elements.length == 0) { return ""; //$NON-NLS-1$ } List<Number> list = new ArrayList<Number>(elements.length); for (Float elem : elements) { list.add(elem); } return join(separator, list); } /** * Returns a string with all double values concatenated by a specified * separator. * * @param separator * Separator string. * @param elements * Double array. * @return a concatenated string. */ public static String join(String separator, double... elements) { if (elements == null || elements.length == 0) { return ""; //$NON-NLS-1$ } List<Number> list = new ArrayList<Number>(elements.length); for (Double elem : elements) { list.add(elem); } return join(separator, list); } /** * Returns a formatted string of the specified number. All trailing zeroes * or decimal points will be stripped. * * @param number * Number to convert to a string. * @return A formatted string. */ public static String format(Number number) { String formatted = Double.toString(number.doubleValue()).replaceAll("\\.0+$", "") //$NON-NLS-1$ //$NON-NLS-2$ .replaceAll("(\\.[0-9]*[1-9])0+$", "$1"); //$NON-NLS-1$ //$NON-NLS-2$ return formatted; } /** * Returns a formatted string of the specified object. * * @param number * Object to convert to a string. * @return A formatted string. */ public static String format(Object obj) { if (obj instanceof Number) { return format((Number) obj); } else { return obj.toString(); } } }