MessageFormat的使用和注意

一、用法:

MessageFormat用来格式化一个消息,通常是一个字符串,比如

 示例1:

 String pattern = "my name is {0}, come from {1}";

 Object[] params = new Object[]{"lucy","China"};

 String value = MessageFormat.format(pattern,params);

 

 运行结果:my name is lucy, come from China

 

 示例2:

 String pattern = "my name is '{0}'";

 Object[] params = new Object[]{"lucy"};

 String value = MessageFormat.format(pattern,params);

 

 运行结果:my name is {0}

 

二、注意:

  1.pattern的内容如果包含花括号如{...}或者{sadsa},则应该用单引号括起来。

    例如,String pattern = "变量{0}正确的格式为${XXX}"

         应该写成String pattern = "变量{0}正确的格式为‘${XXX}’",

         否则会报错IllegalArgumentException

 

 

 

你可能感兴趣的:(MessageFormat)