Android String Array Join join(String delimiter, Object... chunks)

Here you can find the source of join(String delimiter, Object... chunks)

Description

Efficiently combines the string representations of the specified objects, delimiting each chunk with the specified delimiter.

Parameter

Parameter Description
delimiter the specified delimiter
chunks the chunks to be combined

Return

the result of the concatenation

Declaration

public static String join(String delimiter, Object... chunks) 

Method Source Code

//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();
    }
}

Related

  1. join(String[] strings, String separator)
  2. join(String[] s, String glue)
  3. join(String delimiter, String[] strings)
  4. join(int[] arr, String delimiter)
  5. join(String[] parts, String delimiter)
  6. join(String separator, Object... texts)
  7. join(String[] track)
  8. join(String delimiter, String[] array)
  9. join(String[] strArray, String separator)