英文:
Java MessageFormat fails to format specific string
问题
以下是翻译好的部分:
我有一个常量声明如下:
protected static final String LOC_RC_ELT = "[id*='rcLine'][id$='paxIndex{0}']";
然后我使用了MessageFormat:
MessageFormat.format(LOC_RC_ELT, paxIndex)
在这种情况下,MessageFormat无法格式化声明为常量的字符串:
而对于其他情况,比如:
LOC_RC_SELECTED_TAB = "[id*=_rc-tabs{0}] .tabSelected";
这可以正常工作:
为什么会出现这种奇怪的行为?我该如何解决?
提前感谢您的帮助。
英文:
I have a constant declared as following:
protected static final String LOC_RC_ELT = "[id*='rcLine'][id$='paxIndex{0}']";
And then I use MessageFormat:
MessageFormat.format(LOC_RC_ELT, paxIndex)
In this case MessageFormat fails to format the string declared as constant:
This is not true for other cases like:
LOC_RC_SELECTED_TAB = "[id*=_rc-tabs{0}] .tabSelected"
This works just fine:
Why I'm getting this weird behavior ? and how can I solve it ?
Thanks in advance.
答案1
得分: 2
奇怪的行为,但是如规定的那样。
查阅文档:
> 在String中,一对单引号可以用来引用除单引号以外的任意字符。例如,模式字符串"'{0}'"
表示字符串"{0}"
,而不是FormatElement。一个单引号本身必须通过在String中表示为两个单引号''
。
英文:
Weird behavior, but as specified.
Check the documentation:
> Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'"
represents string "{0}"
, not a FormatElement. A single quote itself must be represented by doubled single quotes ''
throughout a String.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论