Here you can find the source of join(Collection> objs, String delimiter)
Parameter | Description |
---|---|
objs | the array of items to join |
delimiter | the text to place between each element in the array, cannot be <code>null</code>. To join without a delimiter, use an empty string. |
Parameter | Description |
---|---|
NullPointerException | if the received delimiter is null. |
null
if items is null
public static String join(Collection<?> objs, String delimiter)
//package com.java2s; //License from project: Open Source License import java.util.Collection; public class Main { /**/*w ww .j a v a2 s.com*/ * Converts an array of objects to a delimited string representation. * <p> * <<<<<<< HEAD The stirng representation of each element is obtained via * <code>toString()</code>. If a element of the array is <code>null</code> it is * skiped. ======= The string representation of each element is obtained via * <code>toString()</code>. If a element of the array is <code>null</code> it is * skipped. >>>>>>> [FIX] Corrects an off-by-one bug on StringUtil.join() * * @param objs the array of items to join * @param delimiter the text to place between each element in the array, cannot be * <code>null</code>. To join without a delimiter, use an empty string. * @return the resulting string on <code>null</code> if items is <code>null</code> * @throws NullPointerException if the received delimiter is null. */ public static String join(Collection<?> objs, String delimiter) { assert delimiter != null : "Delimiter should not be null"; if (objs == null || objs.size() == 0) { return ""; } StringBuffer buffer = new StringBuffer(); for (Object obj : objs) { if (buffer.length() > 0) { buffer.append(delimiter); } buffer.append(obj); } return buffer.toString(); } /** * Converts an array of integer objects to a delimited string representation. * <p> * * @param ints the array of items to join * @param delimiter the text to place between each element in the array; cannot be * <code>null</code>. To join without a delimiter, use an empty string. * @return the resulting string or <code>null</code> if items is <code>null</code> */ public static String join(int[] ints, String delimiter) { assert delimiter != null : "Delimiter should not be null"; if (ints == null || ints.length == 0) { return ""; } final StringBuffer buffer = new StringBuffer(); for (int obj : ints) { if (buffer.length() > 0) { buffer.append(delimiter); } buffer.append(Integer.toString(obj)); } return buffer.toString(); } /** * Converts an array of objects to a delimited string representation. * <p> * The string representation of each element is obtained via <code>toString()</code>. * If a element of the array is <code>null</code> it is skipped. * * @param objs the array of items to join * @param delimiter the text to place between each element in the array, cannot be * <code>null</code>. To join without a delimiter, use an empty string. * @return the resulting string or <code>null</code> if items is <code>null</code> */ public static String join(final Object[] objs, final String delimiter) { assert delimiter != null : "Delimiter should not be null"; if (objs == null) { return null; } final StringBuffer sb = new StringBuffer(); for (int i = 0; i < objs.length; i++) { final Object o = objs[i]; if (o != null) { if (sb.length() > 0) { sb.append(delimiter); } sb.append(o); } } return sb.toString(); } /** * Converts an array of shorts to a delimited string representation. * <p> * * @param shorts the array of items to join * @param delimiter the text to place between each element in the array; cannot be * <code>null</code>. To join without a delimiter, use an empty string. * @return the resulting string or <code>null</code> if items is <code>null</code> */ public static String join(short[] shorts, String delimiter) { assert delimiter != null : "Delimiter should not be null"; if (shorts == null || shorts.length == 0) { return ""; } StringBuffer buffer = new StringBuffer(); for (int obj : shorts) { if (buffer.length() > 0) { buffer.append(delimiter); } buffer.append(Integer.toString(obj)); } return buffer.toString(); } }