Write code to Concatenate all the passed strings - Java java.lang

Java examples for java.lang:String Join

Requirements

Write code to Concatenate all the passed strings

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String args = "java2s.com";
        System.out.println(fastConcat(args));
    }/*from www .  j  a  v  a2 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 Tutorials