Escape multiple “%” characters in Android

為{幸葍}努か 提交于 2019-12-08 14:36:14

问题


In <string-array name="versions"> I have this beast of an entry (boiled down to a reasonable minimum to reproduce the effect):

<item>100% foo 40%bar</item>

which produces these errors:

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

Adding formatted="false" doesn't change a thing.

<item>100&#37; foo 40&#37;bar</item>

results in the same error messages. WTH?

<item>100% foo 40bar</item>
<item>100 foo 40%bar</item>
<item>100% foo 40%</item>

would all work fine. Escaping it with \% is just ignored resulting in the same error. %% doesn't result in an error but I get %%.


回答1:


Encoding each as a unicode character in the xml works for me:

<string name="test">100\u0025 foo 40\u0025bar</string>



回答2:


The % is a reserved character in XML like <, >, etc. Use %% for each % you are using in the string resource.




回答3:


Using CDATA may work..

<item><![CDATA[100% foo 40%]]></item>


来源:https://stackoverflow.com/questions/9386411/escape-multiple-characters-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!