放置在资源包中时,撇号无法正确转换

use*_*108 83 java resourcebundle

放置在资源包中时,撇号无法正确转换.

key = {0}'s brush is {1} centimeters tall

(e.g. Sam's brush is 4 centimeters tall)
Run Code Online (Sandbox Code Playgroud)

如果我从java.util.ResourceBundle这里的问题格式化上面的键,撇号会被错过?

Rag*_*ram 114

你应该逃避单引号

key = {0}''s brush is {1} centimeters tall
Run Code Online (Sandbox Code Playgroud)


Ral*_*lph 32

我强烈认为问题不是ressource包,而是用于打印消息的MessageFormater:

来自MessageFormater java doc:

在字符串中,''(两个单引号)表示单引号.QuotedString可以包含除单引号之外的任意字符; 周围的单引号被删除.UnquotedString可以包含除单引号和左大括号之外的任意字符.因此,应该导致格式化消息"{0}"的字符串可以写为'''{'0}''或'''{0}'''.

所以你需要写:

{0}''s brush is {1} centimeters tall
Run Code Online (Sandbox Code Playgroud)


Ara*_*ram 6

看看这里javadoc

在字符串中,"''"表示单引号.QuotedString可以包含除单引号之外的任意字符; 周围的单引号被删除.UnquotedString可以包含除单引号和左大括号之外的任意字符.因此,应该导致格式化消息"'{0}'"的字符串可以写为"'''{'0}''"或"''''{0}'''".


Sam*_*age 5

你需要加倍单引号,即{0}的画笔高度为{1}厘米


sor*_*son 5

添加到@Ralph的答案:MessageFormat当你有一个类似的文本时,你会发现这是一件事

text1=It's too late
Run Code Online (Sandbox Code Playgroud)

text2={0}''s too late
Run Code Online (Sandbox Code Playgroud)

text1可能不会通过MessageFormater运行(例如,如果传递或不传递spring,则Spring会有不同的代码路径)text2.因此,如果您使用两个单引号text1,它们可能/将显示为这样.因此,您需要检查是否有任何参数被格式化,并相应地使用一个或两个单引号.


Nes*_*aev 5

如果你完全卡住,就像我一样(以上都没有),你可以用它的Unicode替换撇号:\ u0027.请记住,您始终可以在属性文件中使用UTF符号.