Java - Write code to Concatenate all the passed strings

Requirements

Write code to Concatenate all the passed strings

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String args = "book2s.com";
        System.out.println(fastConcat(args));
    }//w  ww.  j ava 2  s  . c o m

    /**
     * Concatenates all the passed strings
     * @param args The strings to concatentate
     * @return the concatentated string
     */
    public static String fastConcat(CharSequence... args) {
        StringBuilder buff = getStringBuilder();
        for (CharSequence s : args) {
            if (s == null)
                continue;
            buff.append(s);
        }
        return buff.toString();
    }

    /**
     * Acquires and truncates the current thread's StringBuilder.
     * @return A truncated string builder for use by the current thread.
     */
    public static StringBuilder getStringBuilder() {
        return new StringBuilder();
    }

    /**
     * Acquires and truncates the current thread's StringBuilder.
     * @param size the inited size of the stringbuilder
     * @return A truncated string builder for use by the current thread.
     */
    public static StringBuilder getStringBuilder(int size) {
        return new StringBuilder(size);
    }
}

Related Exercise