Java占位符
来源:互联网 发布:手机数据恢复免费 编辑:程序博客网 时间:2024/09/21 09:19
在Java中貌似很少有占位符(placeholder)这个概念,取而代之的是fomat类,另外一些框架也实现了占位符这样的东西。
在Java中有两种占位符%和{},
%后面可以是d、f、s等中间也可以加其他参数。只能用于String类对象中,不能用于MessageFormat类对象。
{}中的数字要与后面的参数位置对应。只能用于MessageFormat类对象中,不能用于String类对象。
总的来说String.format()方法用起来不如MessageFormat.format()方法强大。
具体如何使用可以参考官方API文档
http://docs.oracle.com/javase/7/docs/api/
下面的几个例子仅供参考
import java.text.MessageFormat;import java.util.Date;public class test01 {public static void main(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位访客// %f占位符double d = 1.2;float f = 1.2f;System.out.printf("%f %f", d, f); // prints 1.200000 1.200000// %1$s占位符//%n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格,也可以设为0m,在输出之前放置m个0 System.out.println(String.format("我是%1$s,我来自%2$s,今年%3$s岁", "中国人", "北京","22"));// prints 我是中国人,我来自北京,今年22岁// {}占位符,{}内的数字代表第几个参数,参数从0开始String url = "www.baidu.com";int count = 1000;System.out.println(MessageFormat.format("该网站{0}被访问了 {1} 次.", url, count));// prints 该网站www.baidu.com被访问了 1,000 次.// {}占位符String template = "Welcome {0}! Your last login was {1}";String output = MessageFormat.format(template, new Object[] { "Python",new Date().toString() });System.out.println(output);// prints Welcome Python! Your last login was Fri Oct 10 20:47:00 CST 2014}}
0 0
- Android占位符,Java占位符
- java使用占位符
- Java String 占位符
- java String占位符
- java占位符
- java字符串占位符
- Java占位符
- Java printf()占位符
- Java 占位符
- Java 占位符
- java占位符使用
- java中的占位符
- java基础--占位符
- java字符串占位符
- java android 占位符
- Java 占位符使用
- java 占位符替换
- JAVA字符串占位符替换
- 网络编程学习笔记(freeaddrinfo函数)
- 显卡驱动导致的屏保运行假死
- JAVA_03 String数据类型
- HDOJ 2063 过山车(二分图匹配)
- C++ 基础(一)
- Java占位符
- android4.2 message_MWI (Message Waiting Information)
- 数据查询
- 十步带你安装vsftp
- java数据结构之单链表的简单实现
- ubunto 快捷方式启动软件
- 小白学c++之MFC
- 算法系列-----矩阵(一)-------------单位矩阵的生成
- 重写strcpy() 函数原型