国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java中的MessageFormat.format用法實例

2019-11-26 15:10:00
字體:
來源:轉載
供稿:網友

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 

2、格式化字符串時,兩個單引號才表示一個單引號,單個單引號會被省略,除非中文單引號不會被省略,如:

復制代碼 代碼如下:

String value = MessageFormat.format("oh, {0} is 'a' pig", "ZhangSan"); 
 
System.out.println(value);  // 輸出:oh, ZhangSan is a pig 

給字母a加上單引號,如:

復制代碼 代碼如下:

String value = MessageFormat.format("oh, {0} is ''a'' pig", "ZhangSan");

System.out.println(value);  // 輸出:oh, ZhangSan is 'a' pig

如果需要顯示雙引號要進行轉移,比如:String msg = "oh, {0} is /"a/" pig";
3、單引號會使其后面的占位符均失效,導致直接輸出占位符。

復制代碼 代碼如下:

MessageFormat.format("{0}{1}", 1, 2); // 結果12
MessageFormat.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


3、無論是有引號字符串還是無引號字符串,左花括號都是不支持的,如:
復制代碼 代碼如下:

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); 

  因此若要多次格式同一個模式的字符串,那么創建一個MessageFormat實例在執行格式化操作比較好些

復制代碼 代碼如下:

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);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柳河县| 舟曲县| 三亚市| 沿河| 霍林郭勒市| 龙江县| 密山市| 本溪| 广州市| 通河县| 平利县| 汝城县| 读书| 团风县| 金沙县| 曲沃县| 青浦区| 泽普县| 天台县| 阿拉善右旗| 湘潭县| 双峰县| 凤山市| 南和县| 中宁县| 信宜市| 兰西县| 报价| 蕉岭县| 左云县| 尼玛县| 赤峰市| 吐鲁番市| 兴宁市| 临洮县| 静乐县| 西宁市| 五莲县| 偏关县| 天峨县| 龙江县|