幾種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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM