MessageFormat

Edit

 

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模式(主要部分):

1.FormatElement:
2. { ArgumentIndex }
3. { ArgumentIndex , FormatType }
4. { ArgumentIndex , FormatType , FormatStyle }
5.
6. FormatType:
7. number
8. date
9. time
10. choice(需要使用ChoiceFormat)
11.
12. FormatStyle:
13. short
14. medium
15. long
16. full
17. integer
18. currency
19. percent
20. 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的数字组成,因此可以有好多个,如:

1.    String pig = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}";    
2. Object[] array = new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"};
3. String value = MessageFormat.format(message, array);
4. System.out.println(value);

最终结果是:ABCDEFGHIJKLMNOPQ


2、格式化字符串时,两个单引号才表示一个单引号,单个单引号会被省略,如:

1.    String message = "oh, {0} is 'a' pig";  
2. Object[] array = new Object[]{"ZhangSan"};
3. String value = MessageFormat.format(message, array);
4. System.out.println(value);

最终结果是:oh, ZhangSan is a pig


给字母a加上单引号,如:

1.    String message = "oh, {0} is ''a'' pig";  
2. Object[] array = new Object[]{"ZhangSan"};
3. String value = MessageFormat.format(message, array);
4. System.out.println(value);

最终结果是:oh, ZhangSan is ‘a’ pig


3、单引号会使某个字符或串保持原形。
所以,假如没有特殊要求,一般都是要在正式格式化之前把单引号都去掉,否则会造成不必要的麻烦,如:

1.    String message = "oh, '{0}' is a pig";  
2. Object[] array = new Object[]{"ZhangSan"};
3. String value = MessageFormat.format(message, array);
4. 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);

1. 最终结果是:oh, {0,number,#.#}  is 'a' pig。
2.
3.---
4. 如果像下面这样,就可以正确显示:
5. ```
6. String message = "oh, {0,number,#.#} is a pig";
7. Object[] array = new Object[]{new Double(3.1415)};
8. String value = MessageFormat.format(message, array);
9. System.out.println(value);

最终结果是:oh, 3.1 is a pig


4、无论是有引号字符串还是无引号字符串,左花括号都是不支持的,但支持右花括号显示,如:

1.    String message = "oh, { is a pig";  
2. Object[] array = new Object[]{"ZhangSan"};
3. String value = MessageFormat.format(message, array);
4. System.out.println(value);

最终结果是:异常java.lang.IllegalArgumentException: Unmatched braces in the pattern


右花括号可以显示,如:

1.    String message = "oh, } is a pig";  
2. Object[] array = new Object[]{"ZhangSan"};
3. String value = MessageFormat.format(message, array);
4. System.out.println(value);

最终结果是:oh, } is a pig


关于MessageFormat.format方法:

每调用一次MessageFormat.format方法,都会新创建MessageFormat的一个实例,相当于MessageFormat只使用了一次。MessageFormat类的format方法如下:

1.    public static String format(String pattern, Object ... arguments)   
2. {
3. MessageFormat temp = new MessageFormat(pattern);
4. return temp.format(arguments);
5. }

如果要重复使用某个MessageFormat实例,可以用如下方式:

1.    String message = "oh, {0} is a pig";  
2. MessageFormat messageFormat = new MessageFormat(message);
3. Object[] array = new Object[]{"ZhangSan"};
4. String value = messageFormat.format(array);
5. System.out.println(value);

最终结果是:oh, ZhangSan is a pig

@%2802.JavaSE%29%5BMessageFormat%2CMessageFormat.format%20%5D%0A%23MessageFormat%0A%0A%0A%20MessageFormat%u7528%u6765%u683C%u5F0F%u5316%u4E00%u4E2A%u6D88%u606F%uFF0C%u901A%u5E38%u662F%u4E00%u4E2A%u5B57%u7B26%u4E32%uFF0C%u6BD4%u5982%uFF1A%0A%0A%60String%20str%20%3D%20%22I%27m%20not%20a%20%7B0%7D%2C%20age%20is%20%7B1%2Cnumber%2Cshort%7D%22%2C%20height%20is%20%7B2%2Cnumber%2C%23.%23%7D%3B%60%0A%0A%u800CMessageFormat%u53EF%u4EE5%u683C%u5F0F%u5316%u8FD9%u6837%u7684%u6D88%u606F%uFF0C%u7136%u540E%u5C06%u683C%u5F0F%u5316%u540E%u7684%u5B57%u7B26%u4E32%u63D2%u5165%u5230%u6A21%u5F0F%u4E2D%u7684%u9002%u5F53%u4F4D%u7F6E%uFF0C%0A%0A%u6BD4%u5982%uFF1A%0A%u5C06str%u4E2D%u7684%7B0%7D%u7528%22pig%22%u66FF%u6362%uFF0C%7B1%2Cnumber%2Cshort%7D%u7528%u6570%u5B578%u66FF%u6362%uFF0C%7B2%2Cnumber%2C%23.%23%7D%u7528%u6570%u5B571.2%u66FF%u6362%u3002%0A%u90A3%u4E48%u6700%u7EC8%u7528%u6237%u5F97%u5230%u7684%u662F%u4E00%u4E2A%u683C%u5F0F%u5316%u597D%u7684%u5B57%u7B26%u4E32%22I%27m%20not%20a%20pig%2C%20age%20is%208%2C%20height%20is%201.2%22%u3002%0A%0AMessageFormat%u672C%u8EAB%u4E0E%u8BED%u8A00%u73AF%u5883%u65E0%u5173%uFF0C%u800C%u4E0E%u7528%u6237%u63D0%u4F9B%u7ED9MessageFormat%u7684%u6A21%u5F0F%u548C%u7528%u4E8E%u5DF2%u63D2%u5165%u53C2%u6570%u7684%u5B50%u683C%u5F0F%u6A21%u5F0F%u6709%u5173%uFF0C%u4EE5%u751F%u6210%u9002%u7528%u4E8E%u4E0D%u540C%u8BED%u8A00%u73AF%u5883%u7684%u6D88%u606F%u3002%0A%0A**MessageFormat%u6A21%u5F0F%uFF08%u4E3B%u8981%u90E8%u5206%uFF09%uFF1A%20**%0A%60%60%60%0AFormatElement%3A%0A%20%20%20%20%20%20%20%20%20%7B%20ArgumentIndex%20%7D%0A%20%20%20%20%20%20%20%20%20%7B%20ArgumentIndex%20%2C%20FormatType%20%7D%0A%20%20%20%20%20%20%20%20%20%7B%20ArgumentIndex%20%2C%20FormatType%20%2C%20FormatStyle%20%7D%0A%0A%20FormatType%3A%20%0A%20%20%20%20%20%20%20%20%20number%0A%20%20%20%20%20%20%20%20%20date%0A%20%20%20%20%20%20%20%20%20time%0A%20%20%20%20%20%20%20%20%20choice%uFF08%u9700%u8981%u4F7F%u7528ChoiceFormat%uFF09%0A%0A%20FormatStyle%3A%0A%20%20%20%20%20%20%20%20%20short%0A%20%20%20%20%20%20%20%20%20medium%0A%20%20%20%20%20%20%20%20%20long%0A%20%20%20%20%20%20%20%20%20full%0A%20%20%20%20%20%20%20%20%20integer%0A%20%20%20%20%20%20%20%20%20currency%0A%20%20%20%20%20%20%20%20%20percent%0A%20%20%20%20%20%20%20%20%20SubformatPattern%uFF08%u5B50%u6A21%u5F0F%uFF09%0A%60%60%60%0A%20%0A%0A%u8FD8%u4EE5str%u4E3A%u4F8B%uFF0C%u5728%u8FD9%u4E2A%u5B57%u7B26%u4E32%u4E2D%uFF1A%0A%0A1%u3001%7B0%7D%u548C%7B1%2Cnumber%2Cshort%7D%u548C%7B2%2Cnumber%2C%23.%23%7D%3B%u90FD%u5C5E%u4E8EFormatElement%uFF0C0%2C1%2C2%u662FArgumentIndex%u3002%0A%0A2%u3001%7B1%2Cnumber%2Cshort%7D%u91CC%u9762%u7684number%u5C5E%u4E8EFormatType%uFF0Cshort%u5219%u5C5E%u4E8EFormatStyle%u3002%0A%0A3%u3001%7B1%2Cnumber%2C%23.%23%7D%u91CC%u9762%u7684%23.%23%u5C31%u5C5E%u4E8E%u5B50%u683C%u5F0F%u6A21%u5F0F%u3002%0A%0A%20%0A%0A%u6307%u5B9AFormatType%u548CFormatStyle%u662F%u4E3A%u4E86%u751F%u6210%u65E5%u671F%u683C%u5F0F%u7684%u503C%u3001%u4E0D%u540C%u7CBE%u5EA6%u7684%u6570%u5B57%u3001%u767E%u5206%u6BD4%u7C7B%u578B%u7B49%u7B49%u3002%0A%0A%20%0A%0A%23%23%u5B9E%u4F8B%uFF1A%0A%0A1%u3001ArgumentIndex%u5FC5%u987B%u662F%u975E%u8D1F%u6574%u6570%uFF0C%u5B83%u7684%u4E2A%u6570%u4E0D%u53EA%u9650%u4E8E0%u52309%u8FD910%u4E2A%uFF0C%u5B83%u53EF%u4EE5%u75280%u52309%u7684%u6570%u5B57%u7EC4%u6210%uFF0C%u56E0%u6B64%u53EF%u4EE5%u6709%u597D%u591A%u4E2A%uFF0C%u5982%uFF1A%0A%60%60%60%0A%20%20%20%20String%20pig%20%3D%20%22%7B0%7D%7B1%7D%7B2%7D%7B3%7D%7B4%7D%7B5%7D%7B6%7D%7B7%7D%7B8%7D%7B9%7D%7B10%7D%7B11%7D%7B12%7D%7B13%7D%7B14%7D%7B15%7D%7B16%7D%22%3B%20%20%20%20%0A%20%20%20%20Object%5B%5D%20array%20%3D%20new%20Object%5B%5D%7B%22A%22%2C%22B%22%2C%22C%22%2C%22D%22%2C%22E%22%2C%22F%22%2C%22G%22%2C%22H%22%2C%22I%22%2C%22J%22%2C%22K%22%2C%22L%22%2C%22M%22%2C%22N%22%2C%22O%22%2C%22P%22%2C%22Q%22%7D%3B%20%20%0A%20%20%20%20String%20value%20%3D%20MessageFormat.format%28message%2C%20array%29%3B%20%20%20%20%20%0A%20%20%20%20System.out.println%28value%29%3B%20%20%0A%60%60%60%0A%u6700%u7EC8%u7ED3%u679C%u662F%uFF1AABCDEFGHIJKLMNOPQ%0A%0A---%0A2%u3001%u683C%u5F0F%u5316%u5B57%u7B26%u4E32%u65F6%uFF0C%u4E24%u4E2A%u5355%u5F15%u53F7%u624D%u8868%u793A%u4E00%u4E2A%u5355%u5F15%u53F7%uFF0C%u5355%u4E2A%u5355%u5F15%u53F7%u4F1A%u88AB%u7701%u7565%uFF0C%u5982%uFF1A%0A%60%60%60%0A%20%20%20%20String%20message%20%3D%20%22oh%2C%20%7B0%7D%20is%20%27a%27%20pig%22%3B%20%20%0A%20%20%20%20Object%5B%5D%20array%20%3D%20new%20Object%5B%5D%7B%22ZhangSan%22%7D%3B%20%20%0A%20%20%20%20String%20value%20%3D%20MessageFormat.format%28message%2C%20array%29%3B%20%20%0A%20%20%20%20System.out.println%28value%29%3B%20%20%0A%60%60%60%0A%u6700%u7EC8%u7ED3%u679C%u662F%uFF1Aoh%2C%20ZhangSan%20is%20a%20pig%0A%0A---%0A%0A%u7ED9%u5B57%u6BCDa%u52A0%u4E0A%u5355%u5F15%u53F7%uFF0C%u5982%uFF1A%0A%60%60%60%0A%20%20%20%20String%20message%20%3D%20%22oh%2C%20%7B0%7D%20is%20%27%27a%27%27%20pig%22%3B%20%20%0A%20%20%20%20Object%5B%5D%20array%20%3D%20new%20Object%5B%5D%7B%22ZhangSan%22%7D%3B%20%20%20%0A%20%20%20%20String%20value%20%3D%20MessageFormat.format%28message%2C%20array%29%3B%20%20%0A%20%20%20%20System.out.println%28value%29%3B%20%20%0A%60%60%60%0A%20%u6700%u7EC8%u7ED3%u679C%u662F%uFF1Aoh%2C%20ZhangSan%20is%20%27a%27%20pig%0A%0A----%0A3%u3001%u5355%u5F15%u53F7%u4F1A%u4F7F%u67D0%u4E2A%u5B57%u7B26%u6216%u4E32%u4FDD%u6301%u539F%u5F62%u3002%0A%u6240%u4EE5%uFF0C%u5047%u5982%u6CA1%u6709%u7279%u6B8A%u8981%u6C42%uFF0C%u4E00%u822C%u90FD%u662F%u8981%u5728%u6B63%u5F0F%u683C%u5F0F%u5316%u4E4B%u524D%u628A%u5355%u5F15%u53F7%u90FD%u53BB%u6389%uFF0C%u5426%u5219%u4F1A%u9020%u6210%u4E0D%u5FC5%u8981%u7684%u9EBB%u70E6%uFF0C%u5982%uFF1A%0A%60%60%60%0A%20%20%20%20String%20message%20%3D%20%22oh%2C%20%27%7B0%7D%27%20is%20a%20pig%22%3B%20%20%0A%20%20%20%20Object%5B%5D%20array%20%3D%20new%20Object%5B%5D%7B%22ZhangSan%22%7D%3B%20%20%0A%20%20%20%20String%20value%20%3D%20MessageFormat.format%28message%2C%20array%29%3B%20%20%0A%20%20%20%20System.out.println%28value%29%3B%20%20%0A%60%60%60%0A%20%u6700%u7EC8%u7ED3%u679C%u662F%uFF1Aoh%2C%20%7B0%7D%20is%20%27a%27%20pig%uFF0C%u6B64%u5904ZhangSan%u65E0%u6CD5%u663E%u793A%u3002%0A%0A---%0A%20%u53C8%u5982%uFF0C%u4F7F%u7528%u5B50%u683C%u5F0F%u6A21%u5F0F%uFF0C%u591A%u4E86%u4E00%u4E2A%u5355%u5F15%u53F7%uFF1A%0A%20%60%60%60%0A%20%20%20%20String%20message%20%3D%20%22oh%2C%20%27%7B0%2Cnumber%2C%23.%23%7D%20is%20a%20pig%22%3B%20%20%0A%20%20%20%20Object%5B%5D%20array%20%3D%20new%20Object%5B%5D%7Bnew%20Double%283.1415%29%7D%3B%20%20%0A%20%20%20%20String%20value%20%3D%20MessageFormat.format%28message%2C%20array%29%3B%20%20%0A%20%20%20%20System.out.println%28value%29%3B%20%20%0A%60%60%60%0A%20%u6700%u7EC8%u7ED3%u679C%u662F%uFF1Aoh%2C%20%7B0%2Cnumber%2C%23.%23%7D%20%20is%20%27a%27%20pig%u3002%0A%0A---%0A%20%u5982%u679C%u50CF%u4E0B%u9762%u8FD9%u6837%uFF0C%u5C31%u53EF%u4EE5%u6B63%u786E%u663E%u793A%uFF1A%0A%20%60%60%60%0A%20%20%20%20String%20message%20%3D%20%22oh%2C%20%7B0%2Cnumber%2C%23.%23%7D%20is%20a%20pig%22%3B%20%20%0A%20%20%20%20Object%5B%5D%20array%20%3D%20new%20Object%5B%5D%7Bnew%20Double%283.1415%29%7D%3B%20%20%20%20%0A%20%20%20%20String%20value%20%3D%20MessageFormat.format%28message%2C%20array%29%3B%20%20%20%20%0A%20%20%20%20System.out.println%28value%29%3B%20%20%0A%60%60%60%0A%20%u6700%u7EC8%u7ED3%u679C%u662F%uFF1Aoh%2C%203.1%20is%20a%20pig%0A%0A---%0A4%u3001%u65E0%u8BBA%u662F%u6709%u5F15%u53F7%u5B57%u7B26%u4E32%u8FD8%u662F%u65E0%u5F15%u53F7%u5B57%u7B26%u4E32%uFF0C%u5DE6%u82B1%u62EC%u53F7%u90FD%u662F%u4E0D%u652F%u6301%u7684%uFF0C%u4F46%u652F%u6301%u53F3%u82B1%u62EC%u53F7%u663E%u793A%uFF0C%u5982%uFF1A%0A%60%60%60%0A%20%20%20%20String%20message%20%3D%20%22oh%2C%20%7B%20is%20a%20pig%22%3B%20%20%0A%20%20%20%20Object%5B%5D%20array%20%3D%20new%20Object%5B%5D%7B%22ZhangSan%22%7D%3B%20%20%0A%20%20%20%20String%20value%20%3D%20MessageFormat.format%28message%2C%20array%29%3B%20%20%0A%20%20%20%20System.out.println%28value%29%3B%20%20%0A%60%60%60%0A%20%u6700%u7EC8%u7ED3%u679C%u662F%uFF1A%u5F02%u5E38java.lang.IllegalArgumentException%3A%20Unmatched%20braces%20in%20the%20pattern%0A%0A---%0A%u53F3%u82B1%u62EC%u53F7%u53EF%u4EE5%u663E%u793A%uFF0C%u5982%uFF1A%0A%60%60%60%0A%20%20%20%20String%20message%20%3D%20%22oh%2C%20%7D%20is%20a%20pig%22%3B%20%20%0A%20%20%20%20Object%5B%5D%20array%20%3D%20new%20Object%5B%5D%7B%22ZhangSan%22%7D%3B%20%20%0A%20%20%20%20String%20value%20%3D%20MessageFormat.format%28message%2C%20array%29%3B%20%20%0A%20%20%20%20System.out.println%28value%29%3B%20%20%0A%60%60%60%0A%20%u6700%u7EC8%u7ED3%u679C%u662F%uFF1Aoh%2C%20%7D%20is%20a%20pig%0A%0A%20----%0A%u5173%u4E8EMessageFormat.format%u65B9%u6CD5%uFF1A%0A%0A%u6BCF%u8C03%u7528%u4E00%u6B21MessageFormat.format%u65B9%u6CD5%uFF0C%u90FD%u4F1A%u65B0%u521B%u5EFAMessageFormat%u7684%u4E00%u4E2A%u5B9E%u4F8B%uFF0C%u76F8%u5F53%u4E8EMessageFormat%u53EA%u4F7F%u7528%u4E86%u4E00%u6B21%u3002MessageFormat%u7C7B%u7684format%u65B9%u6CD5%u5982%u4E0B%uFF1A%0A%60%60%60%0A%20%20%20%20public%20static%20String%20format%28String%20pattern%2C%20Object%20...%20arguments%29%20%20%20%0A%20%20%20%20%7B%20%20%0A%20%20%20%20%20%20%20%20MessageFormat%20temp%20%3D%20new%20MessageFormat%28pattern%29%3B%20%20%0A%20%20%20%20%20%20%20%20return%20temp.format%28arguments%29%3B%20%20%0A%20%20%20%20%7D%20%20%0A%60%60%60%0A%20%0A%0A----%0A%0A%u5982%u679C%u8981%u91CD%u590D%u4F7F%u7528%u67D0%u4E2AMessageFormat%u5B9E%u4F8B%uFF0C%u53EF%u4EE5%u7528%u5982%u4E0B%u65B9%u5F0F%uFF1A%0A%60%60%60%0A%20%20%20%20String%20message%20%3D%20%22oh%2C%20%7B0%7D%20is%20a%20pig%22%3B%20%20%0A%20%20%20%20MessageFormat%20messageFormat%20%3D%20new%20MessageFormat%28message%29%3B%20%20%0A%20%20%20%20Object%5B%5D%20array%20%3D%20new%20Object%5B%5D%7B%22ZhangSan%22%7D%3B%20%20%0A%20%20%20%20String%20value%20%3D%20messageFormat.format%28array%29%3B%20%20%20%0A%20%20%20%20System.out.println%28value%29%3B%20%20%0A%60%60%60%0A%20%u6700%u7EC8%u7ED3%u679C%u662F%uFF1Aoh%2C%20ZhangSan%20is%20a%20pig

 

posted @ 2017-07-05 14:52  RuntimExcep  阅读(222)  评论(0编辑  收藏  举报