JAVA和C#在占位符方面有些区别,C#提供的占位符是用{num}这种形式,Java需要用%s这种形式,不太习惯,经查发现MessageFormat提供了花括号占位符的功能。
【转自】https://blog.csdn.net/Mint6/article/details/78583316
在Java中貌似很少有占位符(placeholder)这个概念,取而代之的是fomat类,另外一些框架也实现了占位符这样的东西。
在Java中有两种占位符%和{},
%后面可以是d、f、s等中间也可以加其他参数。只能用于String类对象中,不能用于MessageFormat类对象。
{}中的数字要与后面的参数位置对应。只能用于MessageFormat类对象中,不能用于String类对象。
总的来说String.format()方法用起来不如MessageFormat.format()方法强大。
具体如何使用可以参考官方API文档
http://docs.oracle.com/javase/7/docs/api/
下面的几个例子仅供参考
importjava.text.MessageFormat;importjava.util.Date;public classtest01 {public static voidmain(String[] args) {
System.out.println("hello");//print hello//%s占位符,输出字符串
String username = "user1";int num = 3;
System.out.printf("%s您好,您是第%s位访客\n", username, num); //prints user1您好,您是第3位访客/