码迷,mamicode.com
首页 > 其他好文 > 详细

MessageFomat学习

时间:2018-03-30 01:16:49      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:ber   void   tty   mat   显示   public   ati   text   sys   

 

MessageFomat 提供了一种以与语言无关的方式生成连接消息的方法。 用它来构造消息,显示给最终用户。

 1.MessageFormat的格式

 

 MessageFormatPattern:FormatElement ,String

FormatElement: ArgumentIndex,FormatType,FormatStyle

ArgumentIndex:从0开始

FormatType:number,date,time,choice

FormatStyle:short,medium,long,full,integer,currency,percent

 2.例子

 

 

 1 public class TestMessageFormat {
 2     public static void main(String[] args) {
 3         //format中pattern{}格式ArgumentIndex,FormatType,FormatStyle
 4         //---------------ArgumentIndex只存在ArgumentIndex时------------------
 5         String format = MessageFormat.format("{0}", 1);
 6         System.out.println(format);
 7         //单引号一起使用,则忽略‘‘之间的内容
 8         String format1 = MessageFormat.format("‘}{‘0}", 1);
 9         System.out.println("单引号一起使用 \"‘}{‘0}\" ,则忽略‘‘之间的内容: " + format1);
10         //单引号(‘)单独使用,则忽略全部
11         String format2 = MessageFormat.format("‘{0}", 1);
12         System.out.println("单引号(‘)单独使用 \"‘{0}\",则忽略全部: " + format2);
13         //单写右括号(}) 不会报错
14         String format3 = MessageFormat.format("{0}}", 1);
15         System.out.println("单写右括号(}),\"{0}}\" 不会报错: " + format3);
16         //单写左侧括号,会报错 java.lang.IllegalArgumentException: Unmatched braces in the pattern.
17 //        String format4 = MessageFormat.format("{{0}", 1);
18 //        System.out.println(format4);
19         //-----------------同时存在ArgumentIndex,FormatType,FormatStyle时
20         String format4 = MessageFormat.format("{0,number,#.#}", 2.3444);
21         System.out.println(format4);
22     }
23 }

 

 

1
单引号一起使用 "‘}{‘0}" ,则忽略‘‘之间的内容: }{0}
单引号(‘)单独使用 "‘{0}",则忽略全部: {0}
单写右括号(}),"{0}}" 不会报错: 1}
2.3

 

     

 

    

 

MessageFomat学习

标签:ber   void   tty   mat   显示   public   ati   text   sys   

原文地址:https://www.cnblogs.com/coding400/p/8673396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!