Java 中 MessageFormat.format 用法
来源:互联网 发布:淘宝网电脑app下载 编辑:程序博客网 时间:2024/05/16 08:01
原文:http://blog.csdn.net/xiaokui_wingfly/article/details/46124057
MessageFormat本身与语言环境无关,而与用户提供给MessageFormat的模式和用于已插入参数的子格式模式有关,以生成适用于不同语言环境的消息。
MessageFormat模式(主要部分):
FormatElement:
{ ArgumentIndex }:是从0开始的入参位置索引。
{ ArgumentIndex , FormatType }
{ ArgumentIndex , FormatType , FormatStyle }
FormatType: :指定使用不同的Format子类对入参进行格式化处理。值范围如下:
number:调用NumberFormat进行格式化
date:调用DateFormat进行格式化
time:调用DateFormat进行格式化
choice:调用ChoiceFormat进行格式化
FormatStyle::设置FormatType中使用的格式化样式。值范围如下:
short
medium
long
full
integer
currency
percent
SubformatPattern (子格式模式,形如#.##)
还以str为例,在这个字符串中:
1、{0}和{1,number,short}和{2,number,#.#};都属于FormatElement,0,1,2是ArgumentIndex。
2、{1,number,short}里面的number属于FormatType,short则属于FormatStyle。
3、{1,number,#.#}里面的#.#就属于子格式模式。
指定FormatType和FormatStyle是为了生成日期格式的值、不同精度的数字、百分比类型等等。
实例:
1、ArgumentIndex必须是非负整数,它的个数不只限于0到9这10个,它可以用0到9的数字组成,因此可以有好多个,如:
- String msg = "{0}{1}{2}{3}{4}{5}{6}{7}{8}";
- Object [] array = new Object[]{"A","B","C","D","E","F","G","H","I",};
- String value = MessageFormat.format(msg, array);
- System.out.println(value); // 输出:ABCDEFGHI
- String value = MessageFormat.format("oh, {0} is 'a' pig", "ZhangSan");
- System.out.println(value); // 输出:oh, ZhangSan is a pig
- String value = MessageFormat.format("oh, {0} is ''a'' pig", "ZhangSan");
- System.out.println(value); // 输出:oh, ZhangSan is 'a' pig
3、单引号会使其后面的占位符均失效,导致直接输出占位符。
MessageFormat.format("{0}{1}", 1, 2); // 结果12MessageFormat.format("'{0}{1}", 1, 2); // 结果{0}{1}MessageFormat.format("'{0}'-{1}", 1, 2); // 结果{0}-2使用双引号和两个单引号没有关系,比如
- String value = MessageFormat.format("oh, ''{0}'' is a pig", "ZhangSan");
- System.out.println(value); // 输出:oh, 'ZhangSan' is a pig
- String value = MessageFormat.format("oh, {0,number,#.#} is good num", Double.valueOf("3.1415"));
- System.out.println(value); // 输出:oh, 3.1 is good num
- String value = MessageFormat.format("oh, } is good num", Double.valueOf("3.1415"));
- System.out.println(value); // 输出:oh, } is good num
- String value = MessageFormat.format("oh, { is good num", Double.valueOf("3.1415"));
- System.out.println(value); // java.lang.IllegalArgumentException: Unmatched braces in the pattern.
因此要使用到左花括号需要使用单引号配合使用
MessageFormat.format("'{'{0}}", "X-rapido"); // {X-rapido}还有一个有趣的现象,如果出现两个或2个以上左花括号,就会出现分割字符串,但是右花括号就没问题,虽然没有任何意义,实际应用我们也用不到
- String value = MessageFormat.format("oh, {{ is good num", "d");
- System.out.println(value); // oh,
- String value = MessageFormat.format("oh, }} is good num", "d");
- System.out.println(value); // oh, }} is good num
关于MessageFormat.format方法:
每调用一次MessageFormat.format方法,都会新创建MessageFormat的一个实例,相当于MessageFormat只使用了一次。MessageFormat类的format方法如下:
- public static String format(String pattern, Object ... arguments)
- {
- MessageFormat temp = new MessageFormat(pattern);
- return temp.format(arguments);
- }
- String message = "oh, {0} is a pig";
- MessageFormat messageFormat = new MessageFormat(message);
- Object[] array = new Object[]{"ZhangSan"};
- String value = messageFormat.format(array);
- System.out.println(value);
- Java 中 MessageFormat.format 用法
- Java 中 MessageFormat.format 用法
- Java MessageFormat.format的用法
- Java中的MessageFormat.format用法实例
- MessageFormat/String.format用法
- MessageFormat.format()用法
- MessageFormat.format()的用法
- Java MessageFormat.format 特殊符号、单引号
- Java MessageFormat.format和ChoiceFormat
- java.text.MessageFormat.format使用
- Java中的String.format和MessageFormat.format
- MessageFormat.Format的用法,亲测好使!
- String的format用法及MessageFormat的format的用法
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- MessageFormat用法
- MessageFormat用法
- MessageFormat用法
- MessageFormat用法
- html 页面内锚点定位及跳转方法总结
- hdu 1041 Computer Transformation(规律+大数)
- 教你如何批量复制速卖通商品到Shopify自建商店
- 反汇编定位代码崩溃位置_3
- Sublime3下运行含有中文字符的Python程序报错:[Decode error
- Java 中 MessageFormat.format 用法
- Nginx安装配置以及https
- iOS高德地图 指南针(罗盘)不转的解决方法
- 深入理解单例模式
- Swift知识点总结
- cocos studio更新中注意事项
- Kotlin IntelliJ IDEA环境搭建
- 如何快速的开发一个完整的iOS直播app(原理篇)
- c# 读cpu温度,显卡温度,硬盘温度,风扇转速,硬件信息,cpu占用,附赠项目源码