三、MessageFormat子类:
MessageFormat
提供了一种以与语言无关的方式生成连接消息的方法。使用此选项可构建为最终用户显示的消息。
MessageFormat
获取一组对象,格式化它们,然后将格式化的字符串插入到适当位置的模式中。
MessageFormat
与其他Format
类的不同之处在于,您MessageFormat
使用其构造函数之一创建对象(而不是使用getInstance
样式工厂方法)。工厂方法不是必需的,因为MessageFormat
它本身不实现特定于语言环境的行为。任何特定于语言环境的行为都由您提供的模式以及用于插入参数的子格式定义。
MessageFormat
使用以下形式的模式:
MessageFormatPattern: String MessageFormatPattern FormatElement String FormatElement: //在模式字符串中使用一个“{}”来表示FormatElement,也可以用“}”表示一个字符但是不可以用“{” { ArgumentIndex } //ArgumentIndex值是利用通过数字“9”到数字“0”写入一个非负整数 { ArgumentIndex , FormatType } { ArgumentIndex , FormatType , FormatStyle }//FormatType和FormatStyle值被用于创建Format用于格式元素实例。 FormatType: one of number //对应NumberFormat,其子格式对应DecimalFormat指数字 date //对应DateFormat,其子格式对应SimpleDateFormat指日期 time //对应DateFormat,其子格式对应SimpleDateFormat指时间 choice //对应ChoiceFormat FormatStyle: short //DateFormat类里的长度参数 medium //DateFormat类里的长度参数 long //DateFormat类里的长度参数 full //DateFormat类里的长度参数 integer //NumberFormat类里的数字类型integer整型 currency //NumberFormat类里的数字类型currency货币 percent //NumberFormat类里的数字类型percent百分比 SubformatPattern(子模式)
String: //在Sring中对引号的应用:两个单引号(非双引号)表示单引号;单引号表示保持无单引号原形。
StringPart opt 如:Object[] mess=new Object[]{"troye","sivan"};
String StringPart String s="'a' '{0}' ''a'' ''{1}'' ";则显示为a {0} 'a' 'sivan'
MessageFormat类中最重要的两个方法是format和parse,简单来说format(是将消息Object[]类的对象转化为用户所能看懂的字符串形式)parse(是将用户所能看懂的字符串形式转化为消息Object[]类)
format(Object [] arguments,StringBuffer result,FieldPosition pos)格式化一个对象数组,并将MessageFormat格式元素替换为格式化对象的格式附加到提供的格式StringBuffer。
format(String pattern,Object arguments)使用给定模式pattern创建MessageFormat并使用它来格式化给定的参数。
format(Object arguments)在创建MessageFormat对象时初始化了String类的pattern时使用
//arguments - 第一个是要格式化和替换的对象数组。第二个是可变参数,实际上就是一个Object类型的数组。
//result - 附加文本的地方。
//pos - 输入时:如果需要,可以使用对齐字段。输出时:对齐字段的偏移量。
parse(String source,ParsePosition pos)解析字符串
parse(String source)从给定字符串的开头解析文本以生成对象数组。
parseObject(String source,ParsePosition pos)从字符串中解析文本以生成对象数组。
该方法尝试从由给定的索引开始解析文本 pos。如果解析成功,则在pos使用最后一个字符之后将索引更新为索引(解析不一定使用直到字符串末尾的所有字符),并返回解析的对象数组。更新pos可用于指示下一次调用此方法的起点。如果发生错误,则索引pos不会更改,错误索引将pos设置为发生错误的字符的索引,并返回null。
//source- 其中一部分应该被解析。
//pos- 具有索引和错误索引信息的对象,
构造函数:
MessageFormat(String pattern) 为默认语言环境和指定模式构造MessageFormat。
MessageFormat(String pattern,Locale locale) 为指定的语言环境和模式构造MessageFormat。
//pattern - 指定模式
//locale - 指定语言环境
常用方法:
(1)applyPattern()
方法,设置此消息格式所使用的模式
(2)setFormat(
int formatElementIndex,Format newFormat)
方法,使用先前设置的模式字符串中具有给定格式元素索引的format元素设置格式。
(3)setFormatByArgumentIndex
(int argumentIndex, Format newFormat)方法,设置用于先前设置的模式字符串中使用给定参数索引的格式元素的格式。
(4)setFormats(Format[] newFormats)
方法,设置用于先前设置的模式字符串中的格式元素的格式。
(5)setFormatsByArgumentIndex(Format[] newFormats)
方法,设置用于传递给format
方法或从parse
方法返回的值的格式 。
实例:
package codeMessageFormat;
import java.text.*;
import java.util.*;
public class ceshi {
public static void main(String[] args) {
String message1 = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}";
Object[] array1 = new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"};
String value1 = MessageFormat.format(message1, array1);
System.out.println(value1);
String message2 = "oh, {0} is a cool man";//若用{1}替换{0}则输出为oh, {1} is a cool man
Object[] array2 = new Object[]{"troye sivan"};
String value2 = MessageFormat.format(message2, array2);
System.out.println(value2);
String message3 = "oh, {0,number,#.#} is a number";//#.#是FormatStyle里的SubformatPattern(子模式)表示保留一位小数
Object[] array3 = new Object[]{56.125};
String value3 = MessageFormat.format(message3, array3);
System.out.println(value3);
Object[] array4={new Date(),"澳大利亚","晴朗"};
MessageFormat mf= new MessageFormat("当前时间:{0,time},地点:{1},天气:{2}",Locale.US);
String value4=mf.format(array4);
System.out.println(value4);
}
}
运行结果:
ABCDEFGHIJKLMNOPQ
oh, troye sivan is a cool man
oh, 56.1 is a number
当前时间:4:56:11 PM,地点:澳大利亚,天气:晴朗