Here you can find the source of join(String delimiter, Collection
Parameter | Description |
---|---|
delimiter | The delimiter to place between each item. This value may be null, in which case, the empty string will be the delimiter |
items | The items to join. This value may be null, in which case, the empty string will be returned |
public static String join(String delimiter, Collection<String> items)
//package com.java2s; //License from project: Open Source License import java.util.Arrays; import java.util.Collection; public class Main { /**/*from www .ja v a2 s . c o m*/ * The empty string */ public static final String EMPTY = ""; /** * Join one or more items placing the specified delimiter between each item * * @param delimiter * The delimiter to place between each item. This value may be * null, in which case, the empty string will be the delimiter * @param items * The items to join. This value may be null, in which case, the * empty string will be returned * @return A single string that is the combination of the items and the * delimiters */ public static String join(String delimiter, Collection<String> items) { StringBuilder buffer = new StringBuilder(); if (delimiter == null) { delimiter = ""; } if (items != null) { for (String item : items) { if (item != null) { buffer.append(item); } buffer.append(delimiter); } // trim off extra delimiter if (buffer.length() > 0) { buffer.setLength(buffer.length() - delimiter.length()); } } return buffer.toString(); } /** * Join one or more items placing the specified delimiter between each item * * @param delimiter * The delimiter to place between each item. This value may be * null, in which case, the empty string will be the delimiter * @param items * The items to join. This value may be null, in which case, the * empty string will be returned * @return A single string that is the combination of the items and the * delimiters */ public static String join(String delimiter, String... items) { String result; if (items != null) { result = join(delimiter, Arrays.asList(items)); } else { result = EMPTY; } return result; } }