Java - Write code to concatenate the strings and filter out null value.

Requirements

Write code to concatenate the strings and filter out null value.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String strs = "book2s.com";
        System.out.println(concat(strs));
    }//from   w  w w . j a v a  2s .c  o  m

    /**
     * concatenate the strings. Null string convert to blank string.
     * @param strs
     * @return
     * @author <a href="mailto:iffiff1@gmail.com">Tyler Chen</a> 
     * @since 2015-2-6
     */
    public static String concat(String... strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (String str : strs) {
            if (str == null) {
                str = "";
            }
            sb.append(str);
        }
        return sb.toString();
    }
}

Related Exercise