Android String Append concat(Object... chunks)

Here you can find the source of concat(Object... chunks)

Description

Efficiently concatenates the string representations of the specified objects.

Parameter

Parameter Description
chunks the chunks to be concatenated

Return

the result of the concatenation

Declaration

public static String concat(Object... chunks) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w  w w  .j  a v a  2s.  c om
     * Efficiently concatenates the string representations of the
     * specified objects.
     *
     * @param chunks the chunks to be concatenated
     * @return the result of the concatenation
     */
    public static String concat(Object... chunks) {
        return join(null, chunks);
    }

    /**
     * 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. append(final String string, final String append, final String delimiter)
  2. add(String str, int num)
  3. addSuffix(String src, String suffix)
  4. appendStr(Object... strs)