Java 中 MessageFormat.format 用法,messageformat.format

來源:互聯網
上載者:User

Java 中 MessageFormat.format 用法,messageformat.format

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); // 結果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
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); 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.