JAVA学习(十)__MessageFormat用法
MessageFormat用來格式化一個消息,通常是一個字符串,比如:
String str = "I'm not a {0}, age is {1,number,short}", height is {2,number,#.#};
?
而MessageFormat可以格式化這樣的消息,然后將格式化后的字符串插入到模式中的適當位置,比如:
將str中的{0}用"pig"替換,{1,number,short}用數字8替換,{2,number,#.#}用數字1.2替換。
那么最終用戶得到的是一個格式化好的字符串"I'm not a pig, age is 8, height is 1.2"。
?
MessageFormat本身與語言環境無關,而與用戶提供給MessageFormat的模式和用于已插入參數的子格式模式有關,以生成適用于不同語言環境的消息。
?
MessageFormat模式(主要部分):?
?
FormatElement:
???????? { ArgumentIndex }
???????? { ArgumentIndex , FormatType }
???????? { ArgumentIndex , FormatType , FormatStyle }
?
?FormatType:?
???????? number
???????? date
???????? time
???????? choice(需要使用ChoiceFormat)
?
?FormatStyle:
???????? 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 pig = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}"; Object[] array = new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"}; String value = MessageFormat.format(message, array); System.out.println(value);最終結果是:ABCDEFGHIJKLMNOPQ
?
2、格式化字符串時,兩個單引號才表示一個單引號,單個單引號會被省略,如:
String message = "oh, {0} is 'a' pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value);?最終結果是:oh, ZhangSan is a pig
?
給字母a加上單引號,如:
String message = "oh, {0} is ''a'' pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value); ?最終結果是:oh, ZhangSan is 'a' pig?
3、單引號會使某個字符或串保持原形。
???? 所以,假如沒有特殊要求,一般都是要在正式格式化之前把單引號都去掉,否則會造成不必要的麻煩,如:
String message = "oh, '{0}' is a pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value);?最終結果是:oh, {0} is 'a' pig,此處ZhangSan無法顯示。
?
?又如,使用子格式模式,多了一個單引號:
String message = "oh, '{0,number,#.#} is a pig"; Object[] array = new Object[]{new Double(3.1415)}; String value = MessageFormat.format(message, array); System.out.println(value);?最終結果是:oh, {0,number,#.#}? is 'a' pig。
?
?如果像下面這樣,就可以正確顯示:
String message = "oh, {0,number,#.#} is a pig"; Object[] array = new Object[]{new Double(3.1415)}; String value = MessageFormat.format(message, array); System.out.println(value);?最終結果是:oh, 3.1 is a pig
?
3、無論是有引號字符串還是無引號字符串,左花括號都是不支持的,但支持右花括號顯示,如:
String message = "oh, { is a pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value);?最終結果是:異常java.lang.IllegalArgumentException: Unmatched braces in the pattern
?
右花括號可以顯示,如:
String message = "oh, } is a pig"; Object[] array = new Object[]{"ZhangSan"}; String value = MessageFormat.format(message, array); System.out.println(value);?最終結果是:oh, } is a pig
?
?
關于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);?最終結果是:oh, ZhangSan is a pig
轉載于:https://www.cnblogs.com/powerwu/articles/5567460.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的JAVA学习(十)__MessageFormat用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle数据库卸载(需要完全卸载or
- 下一篇: python 3 关于requests库