我可以在 java MessageFormat 中转义大括号吗?

发布于 2024-07-29 08:09:18 字数 216 浏览 3 评论 0原文

我想以 java MessageFormat 输出一些大括号。 例如,我知道以下内容不起作用:

MessageFormat.format("  public {0} get{1}() {return {2};}\n\n", type, upperCamel, lowerCamel);

是否有一种方法可以转义“return {2}”周围的大括号?

I want to output some braces in a java MessageFormat. For example I know the following does not work:

MessageFormat.format("  public {0} get{1}() {return {2};}\n\n", type, upperCamel, lowerCamel);

Is there a way of escaping the braces surrounding "return {2}"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心作怪 2024-08-05 08:09:18

您可以将它们放在单引号内,例如

'{'return {2};'}'

请参阅此处 了解更多详情。

You can put them inside single quotes e.g.

'{'return {2};'}'

See here for more details.

記柔刀 2024-08-05 08:09:18

MessageFormat 的文档知道答案:

字符串中,“''”代表一个
单引号。 QuotedString 可以
包含任意字符,除了
单引号; 周边单身
引号被删除。 一个不带引号的字符串
可以包含任意字符
除了单引号和左卷曲
括号。 因此,一个字符串应该
结果是格式化的消息
"'{0}'" 可以写成 "'''{'0}''"
“'''{0}'''”

The documentation for MessageFormat knows the answer:

Within a String, "''" represents a
single quote. A QuotedString can
contain arbitrary characters except
single quotes; the surrounding single
quotes are removed. An UnquotedString
can contain arbitrary characters
except single quotes and left curly
brackets. Thus, a string that should
result in the formatted message
"'{0}'" can be written as "'''{'0}''"
or "'''{0}'''".

仅此而已 2024-08-05 08:09:18

使用单引号:

MessageFormat.format("  public {0} get{1}() '{'return {2};'}'\n\n",
                     type, upperCamel, lowerCamel);

如果您想实际使用单引号,只需将其加倍即可。 JavaDoc for MessageFormat 给出了这个有点复杂的例子:

因此,一个字符串应该导致
格式化消息 "'{0}'" 可以是
写为“'''{'0}''”或“'''{0}'''”

这是用于单引号的 '',然后是用于转义大括号的 '{',然后是 0'}' 表示右大括号,'' 表示右引号。

Use single quotes:

MessageFormat.format("  public {0} get{1}() '{'return {2};'}'\n\n",
                     type, upperCamel, lowerCamel);

If you want to actually use a single quote, just double it. The JavaDoc for MessageFormat gives this somewhat complicated example:

Thus, a string that should result in
the formatted message "'{0}'" can be
written as "'''{'0}''" or "'''{0}'''".

This is '' for a single quote, then '{' for an escaped brace, then 0, '}' for the closing brace and '' for the closing quote.

天邊彩虹 2024-08-05 08:09:18
System.out.println(MessageFormat.format("I want to see ticks and curly braces around '''{'{0}'}'''", "this"));
System.out.println(MessageFormat.format("I want to see ticks and curly braces around '''{'{0}'}'''", "this"));
弥繁 2024-08-05 08:09:18

您可以将此正则表达式与 perl 或任何其他语言一起使用来转义大括号和单引号 (x27)。 它不会触及任何占位符,例如 {0}

echo "#  'single' quote test \n\n public {0} get{1}() {return {2};}\n\n" | perl -pe 's/\x27/\x27\x27/g; s/\{([^0-9])/\x27\{\x27$1/g; s/([^0-9])\}/$1\x27\}\x27/g'

you can use this regex with perl or any other language to escape curly brackets and single quotes (x27). It does not touch any placeholder e.g. {0}:

echo "#  'single' quote test \n\n public {0} get{1}() {return {2};}\n\n" | perl -pe 's/\x27/\x27\x27/g; s/\{([^0-9])/\x27\{\x27$1/g; s/([^0-9])\}/$1\x27\}\x27/g'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文