zoukankan      html  css  js  c++  java
  • MessageFomat学习

    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

         

        

  • 相关阅读:
    06软件构架实践阅读笔记之六
    05软件构架实践阅读笔记之五
    04软件构架实践阅读笔记之四
    03软件构架实践阅读笔记之三
    02软件构架实践阅读笔记之二
    01软件构架实践阅读笔记之一
    实训第四天
    实训第三天
    实训第二天
    实训第一天
  • 原文地址:https://www.cnblogs.com/coding400/p/8673396.html
Copyright © 2011-2022 走看看