Java中MessageFormat的使用詳解
用于字符串替換,你還在用以下的這種方法嗎?
String.format(String format, Object... args)
這是String類型的靜態(tài)方法,但是除此之外,JDK提供了更好用的字符串替換方法,就是
MessageFormat.format(String pattern, Object ... arguments)
MessageFormat本身與語言環(huán)境無關(guān),而與用戶提供給MessageFormat的模式和用于已插入?yún)?shù)的子格式模式有關(guān),以生成適用于不同語言環(huán)境的消息。
MessageFormat模式(主要部分):
FormatElement:
{ ArgumentIndex }:是從0開始的入?yún)⑽恢盟饕?br /> { ArgumentIndex , FormatType }
{ ArgumentIndex , FormatType , FormatStyle }
FormatType: :指定使用不同的Format子類對(duì)入?yún)⑦M(jìn)行格式化處理。值范圍如下:
number:調(diào)用NumberFormat進(jìn)行格式化
date:調(diào)用DateFormat進(jìn)行格式化
time:調(diào)用DateFormat進(jìn)行格式化
choice:調(diào)用ChoiceFormat進(jìn)行格式化
FormatStyle::設(shè)置FormatType中使用的格式化樣式。值范圍如下:
short
medium
long
full
integer
currency
percent
SubformatPattern (子格式模式,形如#.##)
以str為例,在這個(gè)字符串中:
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是為了生成日期格式的值、不同精度的數(shù)字、百分比類型等等。
實(shí)例
1、ArgumentIndex必須是非負(fù)整數(shù),它的個(gè)數(shù)不只限于0到9這10個(gè),它可以用0到9的數(shù)字組成,因此可以有好多個(gè),如:
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
2、格式化字符串時(shí),兩個(gè)單引號(hào)才表示一個(gè)單引號(hào),單個(gè)單引號(hào)會(huì)被省略,除非中文單引號(hào)不會(huì)被省略,如:
String value = MessageFormat.format("oh, {0} is 'a' pig", "ZhangSan");
System.out.println(value); // 輸出:oh, ZhangSan is a pig
給字母a加上單引號(hào),如:
String value = MessageFormat.format("oh, {0} is ''a'' pig", "ZhangSan");
System.out.println(value); // 輸出:oh, ZhangSan is 'a' pig
如果需要顯示雙引號(hào)要進(jìn)行轉(zhuǎn)移,比如:String msg = “oh, {0} is \”a\” pig”;
3、單引號(hào)會(huì)使其后面的占位符均失效,導(dǎo)致直接輸出占位符。
MessageFormat.format("{0}{1}", 1, 2); // 結(jié)果12
MessageFormat.format("'{0}{1}", 1, 2); // 結(jié)果{0}{1}
MessageFormat.format("'{0}'-{1}", 1, 2); // 結(jié)果{0}-2
使用雙引號(hào)和兩個(gè)單引號(hào)沒有關(guān)系,比如
String value = MessageFormat.format("oh, ''{0}'' is a pig", "ZhangSan");
System.out.println(value); // 輸出:oh, 'ZhangSan' is a pig
又比如,使用子格式模式,多了一個(gè)單引號(hào):
String value = MessageFormat.format("oh, {0,number,#.#} is good num", Double.valueOf("3.1415"));
System.out.println(value); // 輸出:oh, 3.1 is good num
4、無論是有引號(hào)字符串還是無引號(hào)字符串,左花括號(hào)都是不支持的,如:
String value = MessageFormat.format("oh, } is good num", Double.valueOf("3.1415"));
System.out.println(value); // 輸出:oh, } is good num
如果使用左花括號(hào)會(huì)出現(xiàn)異常
String value = MessageFormat.format("oh, { is good num", Double.valueOf("3.1415"));
System.out.println(value); // java.lang.IllegalArgumentException: Unmatched braces in the pattern.
因此要使用到左花括號(hào)需要使用單引號(hào)配合使用
MessageFormat.format(“‘{‘{0}}”, “X-rapido”); // {X-rapido}
還有一個(gè)有趣的現(xiàn)象,如果出現(xiàn)兩個(gè)或2個(gè)以上左花括號(hào),就會(huì)出現(xiàn)分割字符串,但是右花括號(hào)就沒問題,雖然沒有任何意義,實(shí)際應(yīng)用我們也用不到
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
5、關(guān)于MessageFormat.format方法:
每調(diào)用一次MessageFormat.format方法,都會(huì)新創(chuàng)建MessageFormat的一個(gè)實(shí)例,相當(dāng)于MessageFormat只使用了一次。MessageFormat類的format方法如下:
public static String format(String pattern, Object ... arguments)
{
MessageFormat temp = new MessageFormat(pattern);
return temp.format(arguments);
}
因此若要多次格式同一個(gè)模式的字符串,那么創(chuàng)建一個(gè)MessageFormat實(shí)例在執(zhí)行格式化操作比較好些
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);
關(guān)于MessageFormat的詳細(xì)資料請(qǐng)參閱JAVA文檔:http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html
到此這篇關(guān)于Java中MessageFormat的使用詳解的文章就介紹到這了,更多相關(guān)Java MessageFormat內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合ehcache 實(shí)現(xiàn)支付超時(shí)限制的方法
在線支付系統(tǒng)需要極高的穩(wěn)定性,在有限的系統(tǒng)資源下,穩(wěn)定性優(yōu)先級(jí)要高于系統(tǒng)并發(fā)以及用戶體驗(yàn),因此需要合理的控制用戶的支付請(qǐng)求。下面通過本文給大家介紹springboot整合ehcache 實(shí)現(xiàn)支付超時(shí)限制的方法,一起看看吧2018-01-01
Java Calendar類使用總結(jié)及使用實(shí)例
這篇文章主要介紹了Java Calendar類使用總結(jié)及使用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
SpringBoot進(jìn)行參數(shù)校驗(yàn)的方法詳解
在日常的接口開發(fā)中,為了防止非法參數(shù)對(duì)業(yè)務(wù)造成影響,經(jīng)常需要對(duì)接口的參數(shù)進(jìn)行校驗(yàn)。本文通過示例詳細(xì)講解了SpringBoot如何進(jìn)行參數(shù)校驗(yàn)的,感興趣的可以學(xué)習(xí)一下2022-04-04
java.util.Random和concurrent.ThreadLocalRandom使用對(duì)比
這篇文章主要介紹了java.util.Random和concurrent.ThreadLocalRandom使用對(duì)比,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
java 隨機(jī)生成6位短信驗(yàn)證碼實(shí)例代碼
這篇文章主要介紹了java 隨機(jī)生成6位短信驗(yàn)證碼的實(shí)例代碼,文中給大家擴(kuò)展介紹了java隨機(jī)生成四位數(shù)字驗(yàn)證碼的方法,需要的朋友可以參考下2019-12-12
Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析
這篇文章主要介紹了Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析,Ingeter是int的包裝類,int的初值為0,Ingeter的初值為null,int和integer(無論new否)比,都為true,因?yàn)闀?huì)把Integer自動(dòng)拆箱為int再去比,需要的朋友可以參考下2023-12-12
SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解
這篇文章主要介紹了SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java面試題沖刺第十九天--數(shù)據(jù)庫(kù)(4)
這篇文章主要為大家分享了最有價(jià)值的三道關(guān)于數(shù)據(jù)庫(kù)的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-08-08

