使用String.format可以实现字符串的格式化功能,即将后面参数中的值替换掉format中的%s,%d这些值。但MessageFormat更为强大,不用管传入值是字符串还是数字,使用占位符即可。如
String format = "你好,{0},欢迎再次光临{1}!今天是你第{2}次进入。";
String str = MessageFormat.format(format, "小东", "博客园", 5);
System.out.println(str);
打印出来的字符串就是”你好,小东,欢迎再次光临博客园!今天是你第5次进入。“。