简介
MessageFormat最广泛的用法是在字符串中用{}
表示占位符进行填充,常用于错误处理国际化中。{0}表示后面传入参数的第一个参数
使用
format(Object obj)
成员方法
public static void testMessageFormat() {
String object = "程序员职业生涯是{0}和{1}之间";
MessageFormat format = new MessageFormat(object);
String result = format.format(new String[]{"22", "33"});
System.out.println(result);
}
输出:
程序员职业生涯是22和33之间
注意,这里必须将参数放在一个数组中,且数组中的成员有toString()方法可以转换为String类型,即这里的数组不能是基本类型的数组,否则会报java.lang.ClassCastException:
public static void testMessageFormat1() {
String object = "程序员职业生涯是{0}和{1}之间";
MessageFormat format = new MessageFormat(object);
String result = format.format(new int[]{22, 33});
System.out.println(result);
}
输出
java.lang.ClassCastException: class [I cannot be cast to class [Ljava.lang.Object; ([I and [Ljava.lang.Object; are in module java.base of loader 'bootstrap')
at line 885, java.base/java.text.MessageFormat.format
at line 158, java.base/java.text.Format.format
at line 25, Main.testMessageFormat1
at line 5, Main.main
因为format()
是MessageFormat
的抽象父类Format
的方法,里面调用了抽象方法format(Object arguments, StringBuffer result, FieldPosition pos)
/**
* Formats an object to produce a string. This is equivalent to
* <blockquote>
* {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj,
* new StringBuffer(), new FieldPosition(0)).toString();</code>
* </blockquote>
*
* @param obj The object to format
* @return Formatted string.
* @exception IllegalArgumentException if the Format cannot format the given
* object
*/
public final String format (Object obj) {
return format(obj, new StringBuffer(), new FieldPosition(0)).toString();
}
MessageFormat
实现了format(Object arguments, StringBuffer result, FieldPosition pos)
方法,subformat()
方法会将arguments
强制转换为(Object[])
,此时基本类型int[] 本身是一个object对象,不能转为obejct[],所以报ClassCastException
。之后subformat()
方法逐一判断是不是null
、Number
、Date
、String
,如果都不是会尝试调用arg = obj.toString()方法。
public final StringBuffer format(Object arguments, StringBuffer result,
FieldPosition pos)
{
return subformat((Object[]) arguments, result, pos, null);
}
private StringBuffer subformat(Object[] arguments, StringBuffer result,
FieldPosition fp, List<AttributedCharacterIterator> characterIterators) {
// note: this implementation assumes a fast substring & index.
// if this is not true, would be better to append chars one by one.
// 忽略
if (false) { // if (argRecursion == 3){
// prevent loop!!!
result.append('\uFFFD');
} else {
Object obj = arguments[argumentNumber];
String arg = null;
Format subFormatter = null;
if (obj == null) {
arg = "null";
} else if (formats[i] != null) {
subFormatter = formats[i];
if (subFormatter instanceof ChoiceFormat) {
arg = formats[i].format(obj);
if (arg.indexOf('{') >= 0) {
subFormatter = new MessageFormat(arg, locale);
obj = arguments;
arg = null;
}
}
} else if (obj instanceof Number) {
// format number if can
subFormatter = NumberFormat.getInstance(locale);
} else if (obj instanceof Date) {
// format a Date if can
subFormatter = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, locale);//fix
} else if (obj instanceof String) {
arg = (String) obj;
} else {
arg = obj.toString();
if (arg == null) arg = "null";
}
// 忽略
return result;
}
MessageFormat.format(String pattern Object... arguments)
静态方法
public static void testMessageFormat2() {
String object = "程序员职业生涯是{0}和{1}之间";
String result = MessageFormat.format(object,22, 33);
System.out.println(result);
}
输出:
程序员职业生涯是22和33之间