Apostrophe not preceded by \

AndroidXml Parsing

Android Problem Overview


I have a string defined in an android application:

<string name="search_occurs">'{string}' occurs {times}</string>

This gives the error: Apostrophe not preceded by \ (in '{string}' occurs {times})

I've tried a number of things to fix this:

<string name="search_occurs">\'{string}\' occurs {times}</string>
<string name="search_occurs">&apos;{string}&apos; occurs {times}</string>
<string name="search_occurs"><![CDATA['{string}' occurs {times}]]></string>
<string name="search_occurs"><![CDATA[\'{string}\' occurs {times}]]></string>

I'm not able to get rid of the error. What am I missing?

Android Solutions


Solution 1 - Android

these should both work according to http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling:

<string name="search_occurs">\'{string}\' occurs {times}</string>
<string name="search_occurs">"'{string}' occurs {times}"</string>

Solution 2 - Android

just put \ before ' in your string It's FALSE It's TRUE :)

Solution 3 - Android

Why don't you provide the ' with the string you replace it with?

Also you know constructs like this?

<string name="profile_age_range_text_between">between %1$d and %2$d</string>
<string name="unit_height_inches">%1$d\'%2$d ft</string>
<string name="unit_distance_miles">%s mi</string>

Solution 4 - Android

Replace Your apostrophe (') with (') or add \ before every (')

note: copy only inside () text

Solution 5 - Android

Make sure that you are editing the correct file. As this answer states, when you get the error, Android Studio opens a values.xml file, which looks similar to your original file. You have to add the \ in the original strings.xml file (or wherever the offending character is). Otherwise it will just keep repeating the error.

Solution 6 - Android

In answer to https://stackoverflow.com/users/3339394/the-martian There is general solution for replacing ' by ' you have to copy the text into a word editor(like Notepad ++) and then by using the "replace" option in the program replace whole 's with ' . (cause I've not enough reputation i had to write it in an answer sorry!)

Solution 7 - Android

This Error Is Caused By An Apostrophe Used In String

Replace Apostrophe With ' Thats All

Solution 8 - Android

I got the solution based on my scenario.

  1. select your string.xml file
  2. Right click and open show in explorer
  3. Edit the line from the string.xml file and save the code.
  4. Reopen Android studio and refresh the solution.

Now the code will run.

Note: I got the error in using Apostrophe on can't. so i change into Cannot. Now my scenario will works.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrank SchnabelView Question on Stackoverflow
Solution 1 - AndroidJohn BokerView Answer on Stackoverflow
Solution 2 - AndroidReal HyderView Answer on Stackoverflow
Solution 3 - AndroidWarrenFaithView Answer on Stackoverflow
Solution 4 - Androidsaigopi.meView Answer on Stackoverflow
Solution 5 - AndroidSuragchView Answer on Stackoverflow
Solution 6 - AndroidFarhadView Answer on Stackoverflow
Solution 7 - AndroidMuhammad Farooq ArshadView Answer on Stackoverflow
Solution 8 - Androidsudharsan chandrasekaranView Answer on Stackoverflow