String stringFormat = "lexical error at position %s, encountered %s, expected %s ";
String messageFormat ="lexical error at position {0}, encountered {1}, expected {2}";
System.out.println(String.format(stringFormat, 123, 100, 456));
System.out.println(MessageFormat.format(messageFormat, new Date(), 100, 456));
运行结果:

两种方式主要是占位符不一样,如果需要使用高级功能建议是使用MessageFormat。
本文介绍了两种格式化字符串的方法:String.format() 和 MessageFormat.format()。通过示例对比了两者之间的不同,特别是占位符的使用,并指出对于更复杂的需求推荐使用MessageFormat。
6268

被折叠的 条评论
为什么被折叠?



