Java MessageFormat无法格式化特定的字符串

问题描述 投票:-1回答:1

我有一个常量声明如下:

protected static final String LOC_RC_ELT = "[id*='rcLine'][id$='paxIndex{0}']";

然后我使用MessageFormat:

MessageFormat.format(LOC_RC_ELT, paxIndex)

在这种情况下,MessageFormat无法格式化声明为常量的字符串:

enter image description here

对于其他情况,则不是这样:

LOC_RC_SELECTED_TAB = "[id*=_rc-tabs{0}] .tabSelected"

这很好用:

enter image description here

为什么我会有这种奇怪的行为?以及我该如何解决?

提前感谢。

java string-formatting messageformat
1个回答
0
投票

行为异常,但已指定。

检查documentation

String中,一对单引号可用于引用除单引号之外的任何任意字符。例如,模式字符串"'{0}'"表示字符串"{0}",而不是FormatElement。在整个[[String中,单引号本身必须用双引号''表示。

© www.soinside.com 2019 - 2024. All rights reserved.