我在strings.xml文件中写了以下内容:

<string name="game_settings_dragNDropMove_checkBox">Move by Drag&Drop</string>

我得到了以下错误:

The reference to entity "Drop" must end with the ';' delimiter.

我怎么能写字符&在字符串。xml?


当前回答

为了避免错误,使用提取字符串:

<string name="travels_tours_pvt_ltd"><![CDATA[Travels & Tours (Pvt) Ltd.]]></string>

其他回答

我发布这个是因为上面的编码都不适合我。

我发现用序列替换&#38;作为XML字符串资源工作。如:

<string name="webLink"><a href="https://www.example.com?param1=option1&#38;param2=option2">click here</a></string>

应该是这样的:

<string name="game_settings_dragNDropMove_checkBox">Move by Drag&amp;Drop</string>

你可以这样写

<string name="you_me">You &#38; Me<string>

输出:你和我

这可能很古老了。但对于那些寻找快速代码的人。

 public String handleEscapeCharacter( String str ) {
    String[] escapeCharacters = { "&gt;", "&lt;", "&amp;", "&quot;", "&apos;" };
    String[] onReadableCharacter = {">", "<", "&", "\"\"", "'"};
    for (int i = 0; i < escapeCharacters.length; i++) {
        str = str.replace(escapeCharacters[i], onReadableCharacter[i]);
    } return str;
 }

它处理转义字符,您可以在它们各自的数组中添加字符和符号。

干杯

&在字符串中:

<string name="string_abc">Translate &amp; Scan Objects</string>

输出将是:

翻译和扫描对象