亚洲一级免费看,特黄特色大片免费观看播放器,777毛片,久久久久国产一区二区三区四区,欧美三级一区二区,国产精品一区二区久久久久,人人澡人人草

java語言

Java中的MessageFormat.format用法實(shí)例

時(shí)間:2025-02-01 09:28:21 java語言 我要投稿
  • 相關(guān)推薦

Java中的MessageFormat.format用法實(shí)例

  MessageFormat本身與語言環(huán)境無關(guān),而與用戶提供給MessageFormat的模式和用于已插入?yún)?shù)的子格式模式有關(guān),以生成適用于不同語言環(huán)境的消息。下面是小編為大家?guī)淼腏ava中的MessageFormat.format用法實(shí)例,歡迎閱讀。

  Java中的MessageFormat.format用法實(shí)例

  MessageFormat本身與語言環(huán)境無關(guān),而與用戶提供給MessageFormat的模式和用于已插入?yún)?shù)的子格式模式有關(guān),以生成適用于不同語言環(huán)境的消息。

  MessageFormat模式(主要部分):

  代碼如下:

  FormatElement:

  { ArgumentIndex }:是從0開始的.入?yún)⑽恢盟饕?/p>

  { 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

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


【Java中的MessageFormat.format用法實(shí)例】相關(guān)文章:

Java中的多態(tài)用法實(shí)例分析10-23

Java中的Spring入門實(shí)例09-15

Java中synchronized的使用實(shí)例05-31

php中fsockopen用法實(shí)例06-20

Java中Websocket使用實(shí)例解析08-11

ThinkPHP中redirect用法實(shí)例分析09-26

php中return的用法實(shí)例分析10-27

講述java中enum的用法10-14

java中通用的線程池實(shí)例代碼08-27