如何在androidstrings.xml中写字符&
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3053062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write character & in android strings.xml
提问by Palo
I wrote the following in the strings.xml
file:
我在strings.xml
文件中写了以下内容:
<string name="game_settings_dragNDropMove_checkBox">Move by Drag&Drop</string>
I got the following error:
我收到以下错误:
The reference to entity "Drop" must end with the ';' delimiter.
How can I write character & in the strings.xml?
如何在strings.xml 中写入字符&?
回答by Bob Fincheimer
Encode it:
编码它:
&
回答by Moss
For special character I normally use the Unicode definition, for the '&' for example: \u0026 if I am correct. Here is a nice reference page: http://jrgraphix.net/research/unicode_blocks.php?block=0
对于特殊字符,我通常使用 Unicode 定义,对于 '&' 例如: \u0026 如果我是正确的。这是一个不错的参考页面:http: //jrgraphix.net/research/unicode_blocks.php?block=0
回答by Hai Rom
This is a my issues, my solution is as following: Use >
for <
, <
for >
, &
for &
,"'"
for '
, "
for \"\"
这是我的问题,我的解决方案如下:使用>
for <
, <
for >
, &
for &
, "'"
for '
, "
for\"\"
回答by Khemraj
Even your question is answered, still i want tell more entities same like this.
These are html entities
, so in android you will write them like:
即使你的问题得到了回答,我仍然想告诉更多这样的实体。这些是html entities
,所以在 android 中你会这样写:
Replace below with:
下面替换为:
& with &
> with >
< with <
" with ", “ or ”
' with ', ‘ or ’
} with }
回答by faruk
It should be like this :
应该是这样的:
<string name="game_settings_dragNDropMove_checkBox">Move by Drag&Drop</string>
回答by AZ_
You can find all the HTML Special Characters in this page http://www.degraeve.com/reference/specialcharacters.phpJust replace the code where you want to put that character. :-)
您可以在此页面中找到所有 HTML 特殊字符http://www.degraeve.com/reference/specialcharacters.php只需替换要放置该字符的代码即可。:-)
回答by caulitomaz
It is also possible put the contents of your string into a XML CDATA, like Android Studio does for you when you Extract string resource
也可以将字符串的内容放入 XML CDATA 中,就像 Android Studio 为您所做的那样 Extract string resource
<string name="game_settings_dragNDropMove_checkBox"><![CDATA[Move by Drag&Drop]]></string>
<string name="game_settings_dragNDropMove_checkBox"><![CDATA[Move by Drag&Drop]]></string>
回答by ralphgabb
This may be very old. But for those whose looking for a quick code.
这可能很旧了。但是对于那些正在寻找快速代码的人。
public String handleEscapeCharacter( String str ) {
String[] escapeCharacters = { ">", "<", "&", """, "'" };
String[] onReadableCharacter = {">", "<", "&", "\"\"", "'"};
for (int i = 0; i < escapeCharacters.length; i++) {
str = str.replace(escapeCharacters[i], onReadableCharacter[i]);
} return str;
}
That handles escape characters, you can add characters and symbols on their respective arrays.
处理转义字符,您可以在各自的数组上添加字符和符号。
-Cheers
-干杯
回答by A.G.THAMAYS
To avoid the error, use extract string:
为避免错误,请使用提取字符串:
<string name="travels_tours_pvt_ltd"><![CDATA[Travels & Tours (Pvt) Ltd.]]></string>
回答by Hoque MD Zahidul
You can write in this way
你可以这样写
<string name="you_me">You & Me<string>
Output:You & Me
输出:你和我