Java中的MessageFormat.format用法实例
来源:才华咖 本文已影响1.66W人
来源:才华咖 本文已影响1.66W人
MessageFormat本身与语言环境无关,而与用户提供给MessageFormat的模式和用于已插入参数的子格式模式有关,以生成适用于不同语言环境的消息。下面是小编为大家带来的Java中的at用法实例,欢迎阅读。
Java中的at用法实例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 = at(msg, array);
tln(value); // 输出:ABCDEFGHI
2、格式化字符串时,两个单引号才表示一个单引号,单个单引号会被省略,除非中文单引号不会被省略,如:
代码如下:
String value = at("oh, {0} is 'a' pig", "ZhangSan");
tln(value); // 输出:oh, ZhangSan is a pig
给字母a加上单引号,如:
代码如下:
String value = at("oh, {0} is ''a'' pig", "ZhangSan");
tln(value); // 输出:oh, ZhangSan is 'a' pig
如果需要显示双引号要进行转移,比如:String msg = "oh, {0} is "a" pig";
3、单引号会使其后面的占位符均失效,导致直接输出占位符。
代码如下:
at("{0}{1}", 1, 2); // 结果12
at("'{0}{1}", 1, 2); // 结果{0}{1}
at("'{0}'-{1}", 1, 2); // 结果{0}-2
使用双引号和两个单引号没有关系,比如
代码如下:
String value = at("oh, ''{0}'' is a pig", "ZhangSan");
tln(value); // 输出:oh, 'ZhangSan' is a pig
又比如,使用子格式模式,多了一个单引号:
代码如下:
String value = at("oh, {0,number,#.#} is good num", eOf("3.1415"));
tln(value); // 输出:oh, 3.1 is good num
4、无论是有引号字符串还是无引号字符串,左花括号都是不支持的,如:
代码如下:
String value = at("oh, } is good num", eOf("3.1415"));
tln(value); // 输出:oh, } is good num
如果使用左花括号会出现异常
代码如下:
String value = at("oh, { is good num", eOf("3.1415"));
tln(value); // galArgumentException: Unmatched braces in the pattern.
因此要使用到左花括号需要使用单引号配合使用
at("'{'{0}}", "X-rapido"); // {X-rapido}
还有一个有趣的现象,如果出现两个或2个以上左花括号,就会出现分割字符串,但是右花括号就没问题,虽然没有任何意义,实际应用我们也用不到
代码如下:
String value = at("oh, {{ is good num", "d");
tln(value); // oh,
代码如下:
String value = at("oh, }} is good num", "d");
tln(value); // oh, }} is good num
关于at方法:
每调用一次at方法,都会新创建MessageFormat的一个实例,相当于MessageFormat只使用了一次。MessageFormat类的format方法如下:
码如下:
public static String format(String pattern, Object ... arguments)
{
MessageFormat temp = new MessageFormat(pattern);
return at(arguments);
}
因此若要多次格式同一个模式的字符串,那么创建一个MessageFormat实例在执行格式化操作比较好些
代码如下:
String message = "oh, {0} is a pig";
MessageFormat messageFormat = new MessageFormat(message);
Object[] array = new Object[]{"ZhangSan"};
String value = at(array);
tln(value);
My Plan for Summer Vacation英语作文
The Advice From Hollywood Female Stars高中英语作文
The Importance of Breakfast英语作文
At Climate Summit, Hope for Emissions Deal美文欣赏
US Farmers Want to End Cuba Trade Embargo美文欣赏
The Attractions of Video Games大学英语作文
Spring Festival is My Favorite Festival英语作文
RemarksofPresidentBarackObama优秀英语作文
Javascript实例教程如何使用HoTMetal
Margaret Sanger: The Morality of Birth Control quo
A Visittothe Fragrant Mount初中英语作文
whats your plan for dragon boat festival英语作文
CR解析:GMAT - Method of Reasoning 方法类题型
MS SQL Server Management Studio Express安装图文具体步骤
My Favourate Seaso英语作文
My Favourite Season英语作文
The Danger of Fake Commodities英语作文
Efforts Urged to Improve Womens Reproductive Health英语阅读
Myfavoriteprogram英语作文
在java中Synchronized的用法
私家车的坏处The Disadvantages of Private Cars英语作文
英语作文:My favourite Giant pandas
Java学习计划范例
英语阅读:African Union Leaders Talk Mali, Chinese Investmen
My Favourite Season小学英语作文
论合同法中的诚实信用原则
The mountains the most beautiful female teachers英语作文
Dreamweaver中JavaScript行为操作使用的总结
Jolie drops battle over perfume named Shilohy英语作文
My Favourite Season优秀英语作文
should women work after getting married英语作文
My favorite seasons英语作文
关于java Spring整合Freemarker的详细步骤
初中英语作文:Jim Lambert’s vacation plans
Javascript中arguments对象的详解和使用方法
JAVA面试中的陷阱
My favorite animals英语作文
I Can’t Wait For Summer to Come英语作文
Lantern Festival,The 15th day of the 1st lunar month英语作
AssistMyFathertoDoExercises初中英语作文