Apostrophe not preceded by \

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?

82651 次浏览

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>

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>

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

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!)

This Error Is Caused By An Apostrophe Used In String

Replace Apostrophe With \' Thats All

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.

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

note: copy only inside () text

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.