我有一串以下格式
Select * where {{0} rdfs:label "Aruba" } limit 10
Run Code Online (Sandbox Code Playgroud)
现在我想用一些新文本替换{0},但问题是消息格式由于第一个大括号而无法解析字符串.我知道如果我使用'{'它会逃避它,但问题是我有大量这种类型的字符串,我不能手动添加大括号之前和之后的单引号.即使我编写了一个函数来执行此操作,它也会转义占位符{0}的大括号.
它们是消息格式的更好替代品,例如ruby字符串插值.我只想要一种方法来编写一个字符串模板,我可以用新的字符串替换某些部分
我有String模板
xxxxxxxx xxxxx-xx: [{0}] xxxxxxx xxxxx xxxxxx xxxxxx [{1}] xxxxxx xxxx xxxxx'x xxxxx xxxxxx xxxx [{2}]
Run Code Online (Sandbox Code Playgroud)
即使我提供的所有三个参数仍然不起作用
public static void main(String[] args) {
String s = "xxxxxxxx xxxxx-xx: [{0}] xxxxxxx xxxxx xxxxxx xxxxxx [{1}] xxxxxx xxxx xxxxx'x xxxxx xxxxxx xxxx [{2}]";
System.out.println(MessageFormat.format(s,"1","2","3"));
}
Run Code Online (Sandbox Code Playgroud)
输出是:
xxxxxxxx xxxxx-xx: [1] xxxxxxx xxxxx xxxxxx xxxxxx [2] xxxxxx xxxx xxxxxx xxxxx xxxxxx xxxx [{2}]
Run Code Online (Sandbox Code Playgroud)
看输出,它输出{2}而不是3,我找不到为什么它不工作.这是一个错误还是我遗失了什么?
我有像这样的MessageFormat;
final MessageFormat messageFormat = new MessageFormat("This is token one {0}, and token two {1}");
Run Code Online (Sandbox Code Playgroud)
我只是想知道我是否有类似的字符串;
String shouldMatch = "This is token one bla, and token two bla";
String wontMatch = "This wont match the above MessageFormat";
Run Code Online (Sandbox Code Playgroud)
如何检查上述字符串是否是使用messageFormat创建的?即它们匹配messageFormat?
非常感谢!
我在 strings.xml 文件中定义了一个字符串:
<string name="unformatted_string">Here\'s a song I\'ve created just for you\! {0}</string>
Run Code Online (Sandbox Code Playgroud)
然后我获取该字符串并使用 MessageFormat.format,将 {0} 替换为 URL:
String str = MessageFormat.format(getString(R.string.unformatted_string), url);
Run Code Online (Sandbox Code Playgroud)
当我打印 str 时,我看到:
Heres a song Ive created just for you http://www.google.com
Run Code Online (Sandbox Code Playgroud)
MessageFormat文档指出引号的规则有些令人困惑,然后仅提供一个仅使用双引号的示例。从我对文档的阅读来看,我的单引号问题似乎可以通过使用 ''' 来解决:
<string name="unformatted_string">Here'''s a song I'''ve created just for you\! {0}</string>
Run Code Online (Sandbox Code Playgroud)
但是 Android 资源打包步骤会引发错误,因为我有一个撇号,前面没有\.
我唯一的要求是在 strings.xml 中定义字符串(以支持本地化)。
在Java中使用MessageFormat类在字符串中传递不同的变量时
System.out.println(MessageFormat.format("I want to resolve this statement "{ {0}, {1} }", "this", "this"));
Run Code Online (Sandbox Code Playgroud)
当您打印此内容时,它将显示 --> { {0}, {1} } 并且由于包裹在大括号中而不会解析带有值的参数。
月份名称以大写字母而不是小写字母开头,正如它们应该的那样。
我在本地机器上运行的一些示例代码:
Locale portugal = new Locale("pt");
Locale brazil = new Locale("pt", "BR");
Locale france = new Locale("fr", "FR");
Object[] params = new Object[] { new Date() };
String pattern = "Today is {0,date,long}!";
MessageFormat ptFormat = new MessageFormat(pattern, portugal);
MessageFormat brFormat = new MessageFormat(pattern, brazil);
MessageFormat frFormat = new MessageFormat(pattern, france);
StringBuffer ptResult = ptFormat.format(params, new StringBuffer(), new FieldPosition(0));
StringBuffer brResult = brFormat.format(params, new StringBuffer(), new FieldPosition(0));
StringBuffer frResult = frFormat.format(params, new StringBuffer(), null);
System.out.println("Portugal result: " …Run Code Online (Sandbox Code Playgroud) 我有一个像下面这样的字符串.
ABC {一位知名的魔术师}将在{0}的{1}小时内进行表演.
第一对花括号没有占位符.当我将此字符串传递给MessageFormat.format(String,Object [])方法时,如果对象数组包含两个字符串来替换占位符{0}和{1},则会出现以下错误.
java.lang.IllegalArgumentException:所有参数标识符必须是非负数或模式后面的字符串([:ID_Start:] [:ID_Continue:]*).
看来第一对括号正在为第一个占位符进行解析,因为它不是有效占位符,所以会发生错误.
如何告诉MessageFormat.format忽略第一对花括号并与其他两个花括号一起使用?
java replace placeholder messageformat illegalargumentexception
我有一个格式的字符串:
Hello {0}, today is {1} and the time is {2}. Will you be eating {3}?
给定一个数组["sir", "Monday", "12:00", "tacos"],该字符串应格式化为:
Hello sir, today is Monday and the time is 12:00. Will you be eating tacos?
Ruby有没有某种内置方法来做到这一点?还是有宝石?或者我必须更换字符串?
编辑:我想补充一点,我不允许编辑这些字符串.因此类似sprintf的解决方案不起作用.
我有以下 JSON 格式的字符串:
String message = "{ \"message\": \"Hello World!\" }";
Run Code Online (Sandbox Code Playgroud)
但我想将其设置为使用 MessageFormat:
String message = MessageFormat.format("{ \"message\": \"Hello {0}!\" }", "World");
Run Code Online (Sandbox Code Playgroud)
我知道 MessageFormat 使用单引号与反斜杠来转义字符,但我没有看到在这里使用反斜杠的方法,因为我需要在消息中使用双引号,并且如果没有反斜杠,未转义的双引号会破坏字符串。
知道如何让它发挥作用吗?
MessageFormat html = new MessageFormat("<div style='text-align:center;'><img src='file:// {0} ' style='-webkit-transform: rotate({1}deg);'></img></div>");
Run Code Online (Sandbox Code Playgroud)
当我使用时,我无法理解为什么消息格式不能使用此字符串 html.format({"something","someotherthing"});
messageformat ×10
java ×9
android ×1
date ×1
escaping ×1
java-8 ×1
json ×1
localization ×1
placeholder ×1
replace ×1
ruby ×1