How to write character & in android strings.xml

試著忘記壹切 提交于 2019-12-17 04:12:28

问题


I wrote the following in the strings.xml file:

<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?


回答1:


Encode it:

&amp;



回答2:


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




回答3:


This is a my issues, my solution is as following: Use &gt; for <, &lt;for > , &amp; for & ,"'" for ' , &quot for \"\"




回答4:


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:

Replace below with:

& with &amp;
> with &gt;
< with &lt;
" with &quot;, &ldquo; or &rdquo;
' with &apos;, &lsquo; or &rsquo;
} with &#125;



回答5:


You can find all the HTML Special Characters in this page http://www.degraeve.com/reference/specialcharacters.php Just replace the code where you want to put that character. :-)




回答6:


It should be like this :

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



回答7:


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

<string name="game_settings_dragNDropMove_checkBox"><![CDATA[Move by Drag&Drop]]></string>




回答8:


This may be very old. But for those whose looking for a quick code.

 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;
 }

That handles escape characters, you can add characters and symbols on their respective arrays.

-Cheers




回答9:


You can write in this way

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

Output: You & Me




回答10:


To avoid the error, use extract string:

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



回答11:


Mac and Android studio users:

Type your char such as & in the string.xml or layout and choose "Option" and "return" keys. Please refer the screen shot



来源:https://stackoverflow.com/questions/3053062/how-to-write-character-in-android-strings-xml

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