java拼接字符串、格式化字符串方式

        String arg0 = "Bob";
        String arg1 = "Alice";
        System.out.println("hello," + arg0 + ". I am " + arg1 + ".");

        //StringBuilder.append
        StringBuilder builder = new StringBuilder();
        builder.append("hello,");
        builder.append(arg0);
        builder.append(". I am ");
        builder.append(arg1);
        builder.append(".");
        System.out.println(builder.toString());

        //String.format
        String formatStr = String.format("hello,%s. I am %s.", arg0, arg1);
        System.out.println(formatStr);

        //MessageFormat.format
        String formattedText = MessageFormat.format("hello,{0}. I am {1}.", arg0, arg1);
        System.out.println(formattedText);

  java拼接字符串、格式化字符串方式

posted @ 2021-09-11 19:06  浅绿色i  阅读(253)  评论(0编辑  收藏  举报