78

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?

Frank Schnabel
  • 1,156
  • 2
  • 10
  • 14
  • 6
    `\'` works for me. Just tried it in Eclipse. – CommonsWare Mar 29 '13 at 14:51
  • 4
    After I added a backslash in front of apostrophe, it turned to be necessary to `Clean` and rebuild entire project for Eclipse to stop complaining about the error. – Stan Oct 05 '13 at 16:55

8 Answers8

94

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>
John Boker
  • 82,559
  • 17
  • 97
  • 130
  • First one worked for me not tested second one. Great work – VVB Aug 27 '15 at 14:06
  • 2
    both should, and in both cases android studio trims quotes and backslash before build, also it converts \u0027 into single quote and then display error. – El' Sep 13 '16 at 10:20
46

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

Real Hyder
  • 547
  • 5
  • 3
3

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>
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
2

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

note: copy only inside () text

saigopi.me
  • 14,011
  • 2
  • 83
  • 54
2

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.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
0

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

Community
  • 1
  • 1
Farhad
  • 147
  • 2
  • 9
0

This Error Is Caused By An Apostrophe Used In String

Replace Apostrophe With \' Thats All

-1

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.