[英]Java Message Formatter is not working
I have String template 我有String模板
xxxxxxxx xxxxx-xx: [{0}] xxxxxxx xxxxx xxxxxx xxxxxx [{1}] xxxxxx xxxx xxxxx'x xxxxx xxxxxx xxxx [{2}]
Even if I provide all the three arguments still not working 即使我提供的所有三个参数仍然不起作用
public static void main(String[] args) {
String s = "xxxxxxxx xxxxx-xx: [{0}] xxxxxxx xxxxx xxxxxx xxxxxx [{1}] xxxxxx xxxx xxxxx'x xxxxx xxxxxx xxxx [{2}]";
System.out.println(MessageFormat.format(s,"1","2","3"));
}
The output is : 输出是:
xxxxxxxx xxxxx-xx: [1] xxxxxxx xxxxx xxxxxx xxxxxx [2] xxxxxx xxxx xxxxxx xxxxx xxxxxx xxxx [{2}]
See output, Its outputting the {2}
instead of 3
, I cannot find why it is not working. 看输出,它输出
{2}
而不是3
,我找不到为什么它不起作用。 Is it a bug or I am missing something ? 这是一个错误还是我遗失了什么?
Your problem is in the single quote '
you have to use double ''
instead of one : 你的问题在于单引号
'
你必须使用double ''
而不是一个:
xxxxx''x
Read the documentation about single quote ( MessageFormat ) 阅读有关单引号的文档( MessageFormat )
Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes.
在String中,一对单引号可用于引用除单引号之外的任何任意字符。 For example, pattern string "'{0}'" represents string "{0}", not a FormatElement.
例如,模式字符串“'{0}'”表示字符串“{0}”,而不是FormatElement。 A single quote itself must be represented by doubled single quotes '' throughout a String.
单引号本身必须在整个String中用双引号引用''。 For example, pattern string "'{''}'" is interpreted as a sequence of '{ (start of quoting and a left curly brace), '' (a single quote), and }' (a right curly brace and end of quoting), not '{' and '}' (quoted left and right curly braces): representing string "{'}", not "{}".
例如,模式字符串“'{''}'”被解释为'{(引用开始和左大括号),'(单引号)和}'(右大括号和结束)的序列引用),而不是'{'和'}'(引用左右花括号):表示字符串“{'}”,而不是“{}”。
It's the apostrophe indeed, you need to escape it with another apostrophe, like : ''xxx
. 这确实是撇号,你需要用另一个撇号来逃避它,比如:
''xxx
。 Its in the doc btw: 它在doc btw:
Within a String, '' (two single quotes ) represents a single quote.
在字符串中,''(两个单引号)表示单引号。
It's because you have '
in your String. 这是因为你有
'
在你的字符串。 You need to escape it or you are missing one. 你需要逃避它或你错过了一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.