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,#.#}里面的#.#就属于子格式模式。

指定FormatTypeFormatStyle是为了生成日期格式的值、不同精度的数字、百分比类型等等。

实例:

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无法显示。

又如,使用子格式模式,多了一个单引号:

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

最终结果是:oh, {0,number,#.#}  is 'a' pig。

如果像下面这样,就可以正确显示:

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

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

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

  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

转自:http://zqc-0101.iteye.com/blog/1140140

MessageFormat用法的更多相关文章

  1. MessageFormat用法(转载)

    MessageFormat用来格式化一个消息,通常是一个字符串,比如: String str = "I'm not a {0}, age is {1,number,short}", ...

  2. Python基础---->python的使用(二)

    学习一下python,这里对python的基础知识做一个整理.似等了一百年忽而明白,即使再见面,成熟地表演,不如不见. python的一些应用 一.类似于java中的MessageFormat用法 w ...

  3. MessFormat的简单使用

    MessageFormat用法java.text.MessageFormat 作用:MessageFormat 获取一组对象,格式化这些对象,然后将格式化后的字符串插入到模式中的适当位置. Messa ...

  4. MessageFormat.format用法

    用法一: package gacl.request.study; import java.io.IOException; import java.text.MessageFormat; import ...

  5. MessageFormat.format()用法

    1.java.text.Format的继承结构如下   2.MessageFormat模式 FormatElement { ArgumentIndex }:是从0开始的入参位置索引 { Argumen ...

  6. MessageFormat的用法,java动态替换String字符串中的占位符

    import java.text.MessageFormat; import java.util.GregorianCalendar; import java.util.Locale; public ...

  7. String.format和MessageFormat.format的对比用法

    1.MessageFormat.format import java.text.MessageFormat; /** * Created by SYJ on 2017/9/13. */ public ...

  8. java MessageFormat.format 用法

    FormatElement: { ArgumentIndex }:是从0开始的入参位置索引. { ArgumentIndex , FormatType } { ArgumentIndex , Form ...

  9. Java关于Properties用法(二)——替换配置文件中的参数

    上一章讲了配置文件的基本用法,虽然上一章已经可以解决一些需求,但还不些不足之处.假如,配置文件里面的字符串有一部分需要经常变动,另外一些不需要,上一章的方法就不方便了,所以这章主要讲如何在配置文件中使 ...

随机推荐

  1. 【uoj264】 NOIP2016—蚯蚓

    http://uoj.ac/problem/264 (题目链接) 题意 n条蚯蚓,时间为m.每单位时间要可以将最长的蚯蚓切成len/2和len-len/2两份,长度为0的蚯蚓不会消失,因为每单位时间所 ...

  2. CodeForces 37E Trial for Chief

    Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Description Having ...

  3. MyCCL特征码定位原理学习

    这段时间学习WEB方面的技术,遇到了木马免杀特征码定位的问题,这里做一下学习笔记. 这里对MyCCL的分块原理做一下探究 对指定文件生成10个切块 对指定的木马进行切块后,文件列表是这样的. 注意这里 ...

  4. GNU CMAKE 笔记

    最近在调试OJ, 忙了4天多, 最后的问题是judge模块不能正常工作. judge 模块就是两个C++源文件, 它的工作是 从数据库获取用户提交的源码 测评 将测评结果写到数据库 测评部分是与数据库 ...

  5. codevs 1013 求先序排列(二叉树遍历)

    传送门 Description 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=8). Input 两个字符串,分别是中序和后序(每行一个) Outp ...

  6. Distance Between Points

    I need some help. I have to create a function that will calculate the distance between points (x1,y1 ...

  7. hdu 3047–Zjnu Stadium(带权并查集)

    题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...

  8. 在linux下安装tesseract-ocr

    1. 在ubuntu下可以自动安装  [html]   技术分享技术分享    sudo apt-get install tesseract-ocr  2.编译安装    a.编译环境: gcc gc ...

  9. Knockoutjs的环境搭建教程

    最近要在项目中使用Knockoutjs,因此今天就首先研究了一下Knockoutjs的环境搭建,并进行了一个简单的测试,需要的朋友可以了解下 最近要在项目中使用Knockoutjs,因此今天就首先研究 ...

  10. maven的环境搭建

    maven环境快速搭建 最近,开发中要用到maven,所以对maven进行了简单的学习. .关于maven是什么东东,请参考其它文章. ----------------准备工作------------ ...