使用MessageFormat替换字符中的占位符

使用String.format可以实现字符串的格式化功能,即将后面参数中的值替换掉format中的%s,%d这些值。但MessageFormat更为强大,不用管传入值是字符串还是数字,使用占位符即可。如

String format = "你好,{0},欢迎再次光临{1}!今天是你第{2}次进入。";
String str = MessageFormat.format(format, "小东", "博客园", 5);
System.out.println(str);

打印出来的字符串就是”你好,小东,欢迎再次光临博客园!今天是你第5次进入。“。

 

posted @ 2016-09-02 16:50  涂墨留香  阅读(2012)  评论(0编辑  收藏  举报