Here you can find the source of join(String delimiter, Object... chunks)
Parameter | Description |
---|---|
delimiter | the specified delimiter |
chunks | the chunks to be combined |
public static String join(String delimiter, Object... chunks)
//package com.java2s; public class Main { /**// www . java 2 s. c o m * Efficiently combines the string representations of the specified * objects, delimiting each chunk with the specified delimiter. * * @param delimiter the specified delimiter * @param chunks the chunks to be combined * @return the result of the concatenation */ public static String join(String delimiter, Object... chunks) { //It's worth doing two passes here to avoid additional allocations, by //being more accurate in size estimation int nChunks = chunks.length; int delimLength = delimiter == null ? 0 : delimiter.length(); int estimate = delimLength * (nChunks - 1); for (Object chunk : chunks) { if (chunk != null) { estimate += chunk instanceof CharSequence ? ((CharSequence) chunk) .length() : 10; /* why not? */ } } StringBuilder sb = new StringBuilder(estimate); for (int i = 0; i < nChunks; i++) { Object chunk = chunks[i]; if (chunk != null) { String string = chunk.toString(); if (string != null && string.length() > 0) { sb.append(string); if ((i + 1) < nChunks && delimiter != null) { sb.append(delimiter); } } } } return sb.toString(); } }