几种string格式化输出的方式

String.format()

    String str = "aaa%sbbb%sccc%s"; // 这种支持很多格式 %s  %d  %f  等
    String format = String.format(str, "111", "222", "333");
    System.out.println(format);
    // 输出 aaa111bbb222ccc333

MessageFormat.format()

    String format1 = MessageFormat.format("aaa{0} bbb {1} ccc {2}", "1111", "2222", "3333");
    System.out.println(format1);
    // 输出 aaa1111 bbb 2222 ccc 3333

StrSubstitutor.replace()

commons.lang3 包 或者commons.lang包中

    Map<String,String> params = new HashMap<>();
    params.put("name","asdf");
    params.put("xxx","jkl");
    String replace = StrSubstitutor.replace("zzz ${name}, xxx ${xxx}", params);
    System.out.println(replace);
    // 输出 zzz asdf, xxx jkl
原文地址:https://www.cnblogs.com/nxzblogs/p/12654600.html