“Java的MessageFormat类”的版本间的差异
来自CloudWiki
(创建页面,内容为“*MessageFormat类提供了与语言无关方式生成连接消息的方式,从而构造向终端用户显示的消息。MessageFormat获取一组对象,格式化…”) |
(→第2种格式) |
||
(未显示同一用户的4个中间版本) | |||
第2行: | 第2行: | ||
*语法形式为: | *语法形式为: | ||
MessageFormat.format(String pattern, Object… arguments) | MessageFormat.format(String pattern, Object… arguments) | ||
− | + | ==第1种格式== | |
− | |||
− | |||
− | |||
− | |||
*例如,语句块1 | *例如,语句块1 | ||
//pattern格式中{0}表示要格式化对象的索引位置 | //pattern格式中{0}表示要格式化对象的索引位置 | ||
第13行: | 第9行: | ||
*运行结果为: | *运行结果为: | ||
Java is very interesting | Java is very interesting | ||
+ | ==第2种格式== | ||
*例如,语句块2 | *例如,语句块2 | ||
− | <nowiki> | + | <nowiki>import java.text.MessageFormat; |
− | + | import java.util.Date; | |
− | + | ||
− | + | public class Test { | |
− | + | ||
− | + | public static void main(String[] args) { | |
− | + | // TODO Auto-generated method stub | |
− | + | String pattern = "在{0}的时候,一场台风导致了{1}间房屋的摧毁和{2}元人民币的损失。"; | |
− | + | Date date = new Date(); | |
− | + | int house=500;float lost = 1000000f; | |
− | + | ||
+ | //Object[] params = {new Date(),500,1000000}; | ||
+ | String content = MessageFormat.format(pattern, date,house,lost); | ||
+ | System.out.println(content); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </nowiki> | ||
*运行结果为: | *运行结果为: | ||
− | + | 在18-3-18 下午4:24的时候,一场台风导致了500间房屋的摧毁和1,000,000元人民币的损失。 | |
+ | *可以看到在对应的占位符上,我们用对象数组中的内容替代了占位符。但是似乎和最开始我们规定的字符串文本有点不同? | ||
+ | |||
+ | 最开始我们规定的文本: | ||
+ | |||
+ | 在2016年1月9日的时候,一场台风导致了500间房屋的摧毁和¥1,000,000元人民币的损失。 | ||
+ | |||
+ | 例1占位符被替代后的文本: | ||
+ | |||
+ | 在16-1-9 下午7:56的时候,一场台风导致了500间房屋的摧毁和1,000,000元人民币的损失。 | ||
+ | |||
+ | 可以看到日期格式和货币格式跟我们规定的不同,而这一点可以想到前一篇博客我们学到的DateFormat和NumberFormat中可以指定日期/时间或货币的输出格式。而这里我们要先了解下占位符: | ||
+ | |||
+ | 占位符有三种书写格式: | ||
+ | |||
+ | #{index}:index为0~9之间的数字,对应对象数组中的位置。 | ||
+ | |||
+ | #{index,formatType}:index为0~9之间的数字,formatType为参数的格式化类型 | ||
+ | |||
+ | #{index,formatType,formatStyle}:index为0~9之间的数字,formatType为参数的格式化类型,formatStyle表示参数的样式,比如int,long等 | ||
+ | |||
+ | ==第3种格式== | ||
+ | *例3: | ||
+ | <nowiki>import java.text.MessageFormat; | ||
+ | import java.util.Date; | ||
+ | |||
+ | //import java.text.MessageFormat; | ||
+ | public class Test { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | // TODO Auto-generated method stub | ||
+ | //String pattern = "在{0}的时候,一场台风导致了{1}间房屋的摧毁和{2}元人民币的损失。"; | ||
+ | String pattern = "在{0,date,long}的时候,一场台风导致了{1}间房屋的摧毁和{2,number,currency}元人民币的损失。"; | ||
+ | |||
+ | Date date = new Date(); | ||
+ | int house=500;float lost = 1000000f; | ||
+ | String content = MessageFormat.format(pattern, date,house,lost); | ||
+ | System.out.println(content); | ||
+ | } | ||
+ | |||
+ | }</nowiki> | ||
+ | 参考文档 : https://www.cnblogs.com/fjdingsd/p/5143625.html | ||
返回 [[Java程序设计]] | 返回 [[Java程序设计]] |
2020年2月1日 (六) 12:54的最新版本
- MessageFormat类提供了与语言无关方式生成连接消息的方式,从而构造向终端用户显示的消息。MessageFormat获取一组对象,格式化这些对象,然后将格式化后的字符串插入到模式中的适当位置。
- 语法形式为:
MessageFormat.format(String pattern, Object… arguments)
第1种格式
- 例如,语句块1
//pattern格式中{0}表示要格式化对象的索引位置 String pattern = "{0} is very interesting"; System.out.println(MessageFormat.format(pattern, "Java"));
- 运行结果为:
Java is very interesting
第2种格式
- 例如,语句块2
import java.text.MessageFormat; import java.util.Date; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub String pattern = "在{0}的时候,一场台风导致了{1}间房屋的摧毁和{2}元人民币的损失。"; Date date = new Date(); int house=500;float lost = 1000000f; //Object[] params = {new Date(),500,1000000}; String content = MessageFormat.format(pattern, date,house,lost); System.out.println(content); } }
- 运行结果为:
在18-3-18 下午4:24的时候,一场台风导致了500间房屋的摧毁和1,000,000元人民币的损失。
- 可以看到在对应的占位符上,我们用对象数组中的内容替代了占位符。但是似乎和最开始我们规定的字符串文本有点不同?
最开始我们规定的文本:
在2016年1月9日的时候,一场台风导致了500间房屋的摧毁和¥1,000,000元人民币的损失。
例1占位符被替代后的文本:
在16-1-9 下午7:56的时候,一场台风导致了500间房屋的摧毁和1,000,000元人民币的损失。
可以看到日期格式和货币格式跟我们规定的不同,而这一点可以想到前一篇博客我们学到的DateFormat和NumberFormat中可以指定日期/时间或货币的输出格式。而这里我们要先了解下占位符:
占位符有三种书写格式:
#{index}:index为0~9之间的数字,对应对象数组中的位置。
#{index,formatType}:index为0~9之间的数字,formatType为参数的格式化类型
#{index,formatType,formatStyle}:index为0~9之间的数字,formatType为参数的格式化类型,formatStyle表示参数的样式,比如int,long等
第3种格式
- 例3:
import java.text.MessageFormat; import java.util.Date; //import java.text.MessageFormat; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub //String pattern = "在{0}的时候,一场台风导致了{1}间房屋的摧毁和{2}元人民币的损失。"; String pattern = "在{0,date,long}的时候,一场台风导致了{1}间房屋的摧毁和{2,number,currency}元人民币的损失。"; Date date = new Date(); int house=500;float lost = 1000000f; String content = MessageFormat.format(pattern, date,house,lost); System.out.println(content); } }
参考文档 : https://www.cnblogs.com/fjdingsd/p/5143625.html 返回 Java程序设计