包含单引号时,MessageFormat不格式化

时间:2012-06-12 11:29:08

标签: java

我传递的文字是{}填充符和文本的组合。我试图用{}填充{}并尝试使用MessageFormat。

String sss = "{0}SomeText{1}\'.{2}SomeText{2}SomeText{0}{0}SomeText{2}{0}SomeText{0}{1}SomeText{0}{2}{0}{0}{1}{0}{2}{0}{0}{2}{0}{0}{1}{0}{2}{0}";

Object[] testArgs = {"nits1", "Nits2","nits#"};

System.out.println(MessageFormat.format(sss,testArgs));

输出

nits1SomeTextNits2.{2}SomeText{2}SomeText{0}{0}SomeText{2}{0}SomeText{0}{1}SomeText{0}{2}{0}{0}{1}{0}{2}{0}{0}{2}{0}{0}{1}{0}{2}{0}

2 个答案:

答案 0 :(得分:30)

单引号必须使用双引号进行转义:

String sss = "{0}SomeText{1}''.{2}S..."

答案 1 :(得分:1)

我的错误是,我没有使用方法'format(..)'

中返回的值

错误代码:

MessageFormat.format(sss, testArgs);
System.out.println(sss);

正确的代码:

String newString = MessageFormat.format(sss, testArgs);
System.out.println(newString);