在Android中转义多个"%"字符

Gis*_*zmo 20 xml arrays android escaping

在<string-array name ="versions">我有一个条目的野兽(煮沸到合理的最小值来重现效果):

<item>100% foo 40%bar</item>
Run Code Online (Sandbox Code Playgroud)

产生这些错误:

Multiple annotations found at this line:
- error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
- error: Found tag </item> where </string-array> is expected
Run Code Online (Sandbox Code Playgroud)

添加formatted ="false"不会改变任何事情.

<item>100&#37; foo 40&#37;bar</item>
Run Code Online (Sandbox Code Playgroud)

导致相同的错误消息.WTH?

<item>100% foo 40bar</item>
<item>100 foo 40%bar</item>
<item>100% foo 40%</item>
Run Code Online (Sandbox Code Playgroud)

一切都会好起来的.用\%转义它只是被忽略导致相同的错误.%%不会导致错误,但我得到%%.

Mar*_*k D 23

将每个编码为xml中的unicode字符对我有用:

<string name="test">100\u0025 foo 40\u0025bar</string>
Run Code Online (Sandbox Code Playgroud)

  • 我在这上面失去了20分钟,所以我希望能为别人节省时间.要使用String格式化,我必须使用例如,分数:<b>%.2f %% </ b>.两个%字符,附加到格式.以上都不适用于这种情况. (3认同)
  • 我不建议用unicode字符替换%.它们已编码但未转义.如果你的句子中有参数,如<string name ="test">百分比值:%1 $ d\u0025 </ string>并调用"int testValue = 29; String text = getResources().getString(R.string .test,testValue);" 你的应用程序只会崩溃...而是使用%%(或最终<string name ="test">值%1 $ d\u0025\u0025 </ string>会起作用但真的很脏) (2认同)

Ali*_*har 23

%是XML中的保留字符,如<,>等.%%用于%字符串资源中使用的每个字符.

  • %当然不是XML中的保留字符; 此内容限制特定于Android资源文件. (5认同)
  • `%%` 是唯一对我有用的。这是我的最后一个字符串 `(%1$s%%mission)`,例如在 `(2.56% Commission)` 中翻译 (4认同)
  • 你的答复稍微迟了但是还是谢谢:DI如果我很快就会开始尝试Android,我可能会测试你的解决方案,但考虑到我已经尝试了`&#37;`我虽然已经做了足够的回避.`\ %%`绝对比'\ u0025`更好......好吧......也许不是"绝对".:) (3认同)