转载于:http://zqc-0101.iteye.com/blog/1140140 MessageFormat用法

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

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

实例:

1、ArgumentIndex必须是非负整数,它的个数不只限于0到9这10个,它可以用0到9的数字组成,因此可以有好多个,如:

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

String pig = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}";

Object[] array = new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"};

String value = MessageFormat.format(message, array);

System.out.println(value);最终结果是:ABCDEFGHIJKLMNOPQ

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

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

String message = "oh, {0} is 'a' pig";

Object[] array = new Object[]{"ZhangSan"};

String value = MessageFormat.format(message, array);

System.out.println(value); 最终结果是:oh, ZhangSan is a pig

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

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

String message = "oh, {0} is ''a'' pig";

Object[] array = new Object[]{"ZhangSan"};

String value = MessageFormat.format(message, array);

System.out.println(value); 最终结果是:oh, ZhangSan is 'a' pig

3、单引号会使某个字符或串保持原形。

所以,假如没有特殊要求,一般都是要在正式格式化之前把单引号都去掉,否则会造成不必要的麻烦,如:

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

String message = "oh, '{0}' is a pig";

Object[] array = new Object[]{"ZhangSan"};

String value = MessageFormat.format(message, array);

System.out.println(value); 最终结果是:oh, {0} is 'a' pig,此处ZhangSan无法显示。

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

Java代码  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);

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); 最终结果是:oh, {0,number,#.#}  is 'a' pig。

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

Java代码  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);

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); 最终结果是:oh, 3.1 is a pig

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

Java代码  String message = "oh, { is a pig";     Object[] array = new Object[]{"ZhangSan"};     String value = MessageFormat.format(message, array);     System.out.println(value);

String message = "oh, { is a pig";

Object[] array = new Object[]{"ZhangSan"};

String value = MessageFormat.format(message, array);

System.out.println(value); 最终结果是:异常java.lang.IllegalArgumentException: Unmatched braces in the pattern

右花括号可以显示,如:

Java代码  String message = "oh, } is a pig";     Object[] array = new Object[]{"ZhangSan"};     String value = MessageFormat.format(message, array);     System.out.println(value);

String message = "oh, } is a pig";

Object[] array = new Object[]{"ZhangSan"};

String value = MessageFormat.format(message, array);

System.out.println(value); 最终结果是:oh, } is a pig

关于MessageFormat.format方法:

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

Java代码  public static String format(String pattern, Object ... arguments)    {       MessageFormat temp = new MessageFormat(pattern);       return temp.format(arguments);   }

public static String format(String pattern, Object ... arguments) {     MessageFormat temp = new MessageFormat(pattern);     return temp.format(arguments); }

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

Java代码  String message = "oh, {0} is a pig";     MessageFormat messageFormat = new MessageFormat(message);     Object[] array = new Object[]{"ZhangSan"};     String value = messageFormat.format(array);     System.out.println(value);

String message = "oh, {0} is a pig";

MessageFormat messageFormat = new MessageFormat(message);

Object[] array = new Object[]{"ZhangSan"};

String value = messageFormat.format(array);

System.out.println(value); 最终结果是:oh, ZhangSan is a pig

转载于:https://www.cnblogs.com/liaokailin/archive/2013/03/26/2983016.html

MessageFormat用法相关推荐

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

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

  2. python redis 性能测试台_Redis性能测试

    Redis 性能测试 Redis 性能测试是通过同时执行多个命令实现的.Redis性能测试主要是通过src文件夹下的redis-benchmark来实现(Linux系统下) 语法 redis 性能测试 ...

  3. SpringCloud OpenFeign 远程HTTP服务调用用法与原理

    在 openFeign 未出现前,Spring 提供了 RestTemplate 作为远程服务调用的客户端,提供了多种便捷访问远程 Http 服务的方法,能够大大提高客户端的编写效率.由于文章内容会使 ...

  4. Java中利用MessageFormat对象实现类似C# string.Format方法格式化

    我们在写C#代码的时候常常会使用到string.Format("待格式化字符串{0},{1},....",参数1,参数2,...),来格式化字符串,特别是拼接字符的时候,这种方式使 ...

  5. String.format() 方法用法解说

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. String chargeFlowUrl = _AGENT_URL+ "?agentAcc ...

  6. Java魔法堂:初探MessageFormat.format和ChoiceFormat

    一.前言 刚开始从.net的转向java的时候总觉得 String.format 用得不习惯,希望格式模版会这样 {0}, this is {1}'s cat.{1},this is {0}'s do ...

  7. java lambda 两个冒号_java lambda 表达式中的双冒号的用法说明 ::

    双冒号运算就是java中的[方法引用],[方法引用]的格式是 类名::方法名 注意是方法名哦,后面没有括号"()"哒.为啥不要括号,因为这样的是式子并不代表一定会调用这个方法.这种 ...

  8. java中attribute用法_Java FieldPosition getFieldAttribute()用法及代码示例

    java.text.FieldPosition类的getFieldAttribute()方法用于获取Format.field形式的字段标识符. 用法: public Format.Field getF ...

  9. Handler用法总结

    一.线程通讯问题 1.1 Message.Handler.Looper 在Android中提供了一种异步回调机制Handler,我们可以它来完成一个很长时间的任务. Handler基本使用: 在主线程 ...

最新文章

  1. 有人买不?没人的话我待会儿再来问问 价值6.11亿美元的入侵工具无人问津
  2. 2016去哪儿编程题:乘坐公交
  3. 机器学习理论《统计学习方法》学习笔记:奇异值分解(SVD)
  4. 滥用static_沉思滥用:“强力使用,破坏滥用”
  5. 来自Java空间的传送门
  6. shell获取执行脚本路径
  7. uniapp打包安装后提示_本应用使用HBuilderX 2.8.12 或对应的cli版本编译,而手机端SDK版本是2.8.13---基于Vue的uniapp手机端_前端UI_uview工作笔记007
  8. 环形线圈车辆检测器突破了LOOP-LOCK
  9. Splash特征描述子
  10. java的ArrayList分析
  11. 在FuchsiaOS,AI助手和软件优化上
  12. word onenote_在Word和OneNote中求解和图形方程式
  13. Android开发:ZXing条码扫描-竖屏解决方案
  14. C语言编程圆周运动运行结果,湘潭大学《C语言程序设计Ⅱ》课程考试试卷.doc
  15. 蚁群算法原理及python实现
  16. java计数器生成流水号_CODESOFT打印流水号
  17. Labview的CAN通讯
  18. 计算机无法安装cad怎么办,安装cad时电脑提示已经安装怎么办 cad无法安装的解决方法...
  19. Flutter与RN
  20. 【Spring boot 常见问题】

热门文章

  1. win7清理系统后,音频设备被禁用,没有声音
  2. 几维安全:千锤百炼,锻造移动游戏安全防护黄金铠甲
  3. 分析JQ作者的类实现过程
  4. 绕开“陷阱“,阿里专家带你深入理解C++对象模型的特殊之处
  5. python linux 下开发环境搭建
  6. Jmeter之Constant Timer与constant throughput timer的区别
  7. Tapestry 教程(七)在Tapestry中一起使用Hibernate
  8. 如何利用系统自带的小工具制作特殊字符
  9. Git fatal: write error: Broken pipe
  10. C++ int string 转换