• MessageFormat


    转自:https://blog.csdn.net/woshixuye/article/details/8252391

    一、基本概念

    MessageFormat用来格式化一个消息,通常是一个字符串。MessageFormat模式的主要部分:


    FormatElement:
             { ArgumentIndex }
             { ArgumentIndex , FormatType }
             { ArgumentIndex , FormatType , FormatStyle }

    FormatType: 
             number
             date
             time
             choice(需要使用ChoiceFormat)

     
    FormatStyle:
             short
             medium
             long
             full
             integer
             currency
             percent
             SubformatPattern(子模式)

    {0}、{1,number,short}、{2,number,#.#}属于FormatElement,0,1,2是ArgumentIndex

    {1,number,short}里面的number属于FormatType,short则属于FormatStyle

    {1,number,#.#}里面的#.#就属于子格式模式

    二、例子

    1.  
      import java.text.MessageFormat;
    2.  
       
    3.  
      String str = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}";
    4.  
      Object[] array = new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"};
    5.  
      String value = MessageFormat.format(str, array);
    6.  
      System.out.println(value); // ABCDEFGHIJKLMNOPQ
    7.  
       
    8.  
       
    9.  
      String message = "oh, {0} is a person";
    10.  
      Object[] array = new Object[]{"ZhangSan"};
    11.  
      String value = MessageFormat.format(message, array);
    12.  
      System.out.println(value); // oh, ZhangSan is a person
    13.  
       
    14.  
       
    15.  
      String message = "oh, {0,number,#.#} is a number";
    16.  
      Object[] array = new Object[]{new Double(3.1415)};
    17.  
      String value = MessageFormat.format(message, array);
    18.  
      System.out.println(value); // oh, 3.1 is a number
    19.  
       
    20.  
       
    21.  
      // MessageFormat的format方法源码
    22.  
      public static String format(String pattern, Object ... arguments)
    23.  
      {
    24.  
      MessageFormat temp = new MessageFormat(pattern);
    25.  
      return temp.format(arguments);
    26.  
      }

    对字符串的匹配比较智能

    1.  
      String str = "{0} | {1} | {0} | {1}";
    2.  
      Object[] array = new Object[] { "A", "B" };
    3.  
      String value = MessageFormat.format(str, array);
    4.  
      System.out.println(value); // A | B | A | B

    原帖地址(节选):http://zqc-0101.iteye.com/blog/1140140

  • 相关阅读:
    falsk-web 表单
    falsk-web 表单
    falsk-web 表单
    falsk-web 表单
    治理“假货之都”需要大数据打假
    治理“假货之都”需要大数据打假
    治理“假货之都”需要大数据打假
    治理“假货之都”需要大数据打假
    与好友合伙创业,他开店4家,月销售额已超过30万元
    放弃优越的都市生活,他返乡创业带动家乡人民共同致富
  • 原文地址:https://www.cnblogs.com/sharpest/p/10884761.html
Copyright © 2020-2023  润新知