JAVA学习(十)__MessageFormat用法

本文详细介绍了Java中的MessageFormat类用法,包括如何使用格式元素、格式类型及样式来生成国际化消息,提供了多个示例帮助理解。
 

JAVA学习(十)__MessageFormat用法

分类: java技术备份   4616人阅读  评论(1)  收藏  举报

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,#.#};都属于FormatElement0,1,2是ArgumentIndex。

2、{1,number,short}里面的number属于FormatTypeshort则属于FormatStyle

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

 

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

 

实例:

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

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

最终结果是:ABCDEFGHIJKLMNOPQ

 

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

Java代码   收藏代码
  1. String message = "oh, {0} is 'a' pig";  
  2.   
  3. Object[] array = new Object[]{"ZhangSan"};  
  4.   
  5. String value = MessageFormat.format(message, array);  
  6.   
  7. System.out.println(value);  

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

 

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

Java代码   收藏代码
  1. String message = "oh, {0} is ''a'' pig";  
  2.   
  3. Object[] array = new Object[]{"ZhangSan"};  
  4.   
  5. String value = MessageFormat.format(message, array);  
  6.   
  7. System.out.println(value);  

 最终结果是:oh, ZhangSan is 'a' pig

 

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

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

Java代码   收藏代码
  1. String message = "oh, '{0}' is a pig";  
  2.   
  3. Object[] array = new Object[]{"ZhangSan"};  
  4.   
  5. String value = MessageFormat.format(message, array);  
  6.   
  7. System.out.println(value);  

 最终结果是:oh, {0} is 'a' pig,此处ZhangSan无法显示。

 

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

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

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

 

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

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

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

 

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

Java代码   收藏代码
  1. String message = "oh, { is a pig";  
  2.   
  3. Object[] array = new Object[]{"ZhangSan"};  
  4.   
  5. String value = MessageFormat.format(message, array);  
  6.   
  7. System.out.println(value);  

 最终结果是:异常java.lang.IllegalArgumentException: Unmatched braces in the pattern

 

右花括号可以显示,如:

Java代码   收藏代码
  1. String message = "oh, } is a pig";  
  2.   
  3. Object[] array = new Object[]{"ZhangSan"};  
  4.   
  5. String value = MessageFormat.format(message, array);  
  6.   
  7. System.out.println(value);  

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

 

 

关于MessageFormat.format方法:

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

Java代码   收藏代码
  1. public static String format(String pattern, Object ... arguments)   
  2. {  
  3.     MessageFormat temp = new MessageFormat(pattern);  
  4.     return temp.format(arguments);  
  5. }  

 

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

Java代码   收藏代码
  1. String message = "oh, {0} is a pig";  
  2.   
  3. MessageFormat messageFormat = new MessageFormat(message);  
  4.   
  5. Object[] array = new Object[]{"ZhangSan"};  
  6.   
  7. String value = messageFormat.format(array);  
  8.   
  9. System.out.println(value);  

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


Java 中 `MessageFormat` 类主要用于格式化包含动态参数的字符串,支持国际化等功能,以下是其具体使用方法: ### 基本用法 可以使用静态方法 `format()` 进行格式化,示例如下: ```java String result = java.text.MessageFormat.format("{0} -> {1}", "Hello", "world"); System.out.println(result); // 输出: Hello -> world ``` 上述代码中,使用 `MessageFormat.format` 方法将占位符 `{0}` 和 `{1}` 分别替换为 `Hello` 和 `world` [^2]。 ### 参数替换 在 `MessageFormat` 中,占位符通过索引来指定参数的位置,示例如下: ```java public class MessageFormatExample { public static void main(String[] args) { String message = java.text.MessageFormat.format("name={1}, age={0}, {1}", 25, "huhx"); System.out.println(message); // 输出: name=huhx, age=25, huhx } } ``` 此代码中,`{0}` 被替换为 `25`,`{1}` 被替换为 `huhx` [^4]。 ### 国际化整合 `MessageFormat` 具有较强的国际化支持,可结合资源文件使用。资源文件需保存为 UTF - 8 编码,避免中文等字符乱码。示例如下: ```java import java.text.MessageFormat; import java.util.Locale; import java.util.ResourceBundle; public class I18nExample { public static void main(String[] args) { Locale locale = Locale.CHINA; ResourceBundle bundle = ResourceBundle.getBundle("messages", locale); String pattern = bundle.getString("greeting"); String formattedMessage = MessageFormat.format(pattern, "张三"); System.out.println(formattedMessage); } } ``` 在上述代码中,从资源文件 `messages.properties` 中获取消息模板,然后使用 `MessageFormat` 进行格式化 [^3]。 ### 复杂格式化 `MessageFormat` 支持内置日期、数字等格式化,示例如下: ```java import java.text.MessageFormat; import java.util.Date; public class ComplexFormatExample { public static void main(String[] args) { String pattern = "{0,date,yyyy-MM-dd}"; String formattedDate = MessageFormat.format(pattern, new Date()); System.out.println(formattedDate); } } ``` 此代码将日期对象按照指定的格式进行格式化 [^3]。 ### 与 `String.format()` 对比 `MessageFormat` 和 `String.format()` 各有特点,`MessageFormat` 国际化支持强,支持命名参数、复数形式处理和复杂格式化;而 `String.format()` 需手动指定 `Locale`,参数必须按顺序传递,无直接的复数形式处理 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值