Tin*_*iny 58 java resourcebundle
我已经在资源包中存储了一些消息.我正在尝试格式化这些消息,如下所示.
import java.text.MessageFormat;
String text = MessageFormat.format("You're about to delete {0} rows.", 5);
System.out.println(text);
Run Code Online (Sandbox Code Playgroud)
假设第一个参数即实际消息存储在以某种方式检索的属性文件中.
第二个参数即5是一个动态值,应该放在{0}
不会发生的占位符中.下一行打印,
您即将删除{0}行.
占位符不会替换为实际参数.
这是撇号 - You're
.我试图像往常一样逃避它,就You\\'re
好像它不起作用.要使其发挥作用需要做哪些改变?
Rei*_*eus 103
'
在MessageFormat
图案中添加一个额外的撇号以String
确保'
显示字符
String text =
java.text.MessageFormat.format("You''re about to delete {0} rows.", 5);
^
Run Code Online (Sandbox Code Playgroud)
MessageFormat模式中的撇号(又名单引号)启动带引号的字符串,不会自行解释.来自javadoc
单引号本身必须在整个String中用双引号引用''.
这String
You\\'re
相当于添加一个反斜杠字符,String
因此唯一的区别You\re
将是生成而不是Youre
.(在应用双引号解决方案之前''
)
请确保你使用过双撇号('')
String text = java.text.MessageFormat.format("You''re about to delete {0} rows.", 5);
System.out.println(text);
Run Code Online (Sandbox Code Playgroud)
编辑:
在String中,一对单引号可用于引用除单引号之外的任何任意字符.例如,模式字符串"'{0}'"表示字符串"{0}",而不是FormatElement....
在给定模式的末尾,任何不匹配的引用都被视为已关闭.例如,模式字符串" ' {0}"被视为模式" ' {0} ' ".
来源http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html
你需要在"你是"中使用双撇号而不是单撇号,例如:
String text = java.text.MessageFormat.format("You''re about to delete {0} rows.", 5);
System.out.println(text);
Run Code Online (Sandbox Code Playgroud)