Apostrophe not preceded by \ - android

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?

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

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>

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.

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.
select your string.xml file
Right click and open show in explorer
Edit the line from the string.xml file and save the code.
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.

Related

Android studio won't change string

In my strings.xml file I had this string:
<string name="lets_do_this">Let's Do This</string>
This gave me an error of:
Error:(897) Apostrophe not preceded by \ (in Let's Do This)
So I changed the string to this:
<string name="lets_do_this">Let\'s Do This</string>
But every time I build the project, the string keeps changing back to the first version for some reason and I keep getting that error without the ability to change the string. Why is this happening?
If it changes back, sounds like you changed the strings xml file that is located within the build folder, but you need to edit your own within res/values of the app module.
If you have an apostrophe (') in your string, you must either escape it with a backslash (\') or enclose the string in double-quotes ("").
see
Formatting and Styling - String Resources
<string name="lets_do_this">"Let's Do This"</string>

Error:(218) Apostrophe not preceded by \

In my strings.xml, I have made a string containing (it's) and I get this error.
Error:(218) Apostrophe not preceded by \
Yes, I know I have to either place a \ before the apostrophe, or just delete the apostrophe, but my problem is that when I run the app using (it\s) or just deleting it, it comes back; even if I delete the whole string, it all comes back when I click run. How do I stop this?
Here's the String but it really doesn't matter. I just want it to be deleted and not restored when I run the app.
<string> name="talk" So, you want these things? LOL its gonna</string>
When the error occurs, Android Studio opens the values.xml which, at that point in the file, deceptively looks exactly the same as your strings.xml. Make sure you're updating the strings.xml and you should be good to go.
Actually, there are two files strings.xml and values.xml
We edit our string data in strings.xml and whenever we getting the error like Apostrophe not preceded by \, When you click on error android studio redirect you to values.xml and you are editing in values.xml and android studio just deleted what you edited.
So make sure you are editing strings.xml not values.xml
Place your string in double quotes.<string>"Your text 'here'"</string>
try
<string name="talk">your string value</string>

How to overcome the fault in apostrophe strings.xml in android? [duplicate]

I have declared a long string in string.xml of an application.
Declared like this
<string name="terms">PLEASE READ THESE TERMS OF USE CAREFULLY BY ACCESSING THIS .................</string>
But this gives the following error :
error: Apostrophe not preceded by \ (in PLEASE READ THESE TERMS OF USE CAREFULLY
post your complete string. Though, my guess is there is an apostrophe (') character in your string. replace it with (\') and it will fix the issue. for example,
//strings.xml
<string name="terms">
Hey Mr. Android, are you stuck? Here, I\'ll clear a path for you.
</string>
Ref:
http://www.mrexcel.com/forum/showthread.php?t=195353
https://code.google.com/archive/p/replicaisland/issues/48
Solution
Apostrophes in the strings.xml should be written as
\'
Example
In my case I had an error with this string in my strings.xml and I fixed it.
<item>Most arguments can be ended with three words, "I don\'t care".</item>
Here you see my app builds properly with that code.
Here is the actual string in my app.
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Escaping apostrophes and quotes
If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don't work:
<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>
You have to put \ before an apostrophe. Like this \' , Also check that you are editing strings.xml and not values.xml (android studio directs you to this file when shows the error). Because if you edit values.xml and try to compile again, the error persists. This was happening to me recently.
Use this regex (?<!\\)' for searching an unescaped apostrophe.
It finds an apostrophe that not preceded by a backslash.
I use hebrew(RTL language) in strings.xml.
I have manually searched the string.xml for this char: '
than I added the escape char \ infront of it (now it looks like \' ) and still got the same error!
I searched again for the char ' and I replaced the char ' with \'(eng writing) , since it shows a right to left it looks like that '\ in the strings.xml !!
Problem solved.
The best answer of simply escaping the ' with \' works but I do not like the solution because when using replace all with ' to \', it only works once. The second time you'll be left with \\'.
Therefore, use: \u0027.
You may be able to use unicode equivalent both apostrophe and other characters which are not supported in xml string. Apostrophe's equivalent is "\u0027" .
1. for error: unescaped apostrophe in string
what I found is that AAPT2 tool points to wrong row in xml, sometimes.
So you should correct whole strings.xml file
In Android Studio, in problem file use :
Edit->find->replace
Then write in first field \' (or \&,>,\<,\")
in second put '(or &,>,<,")
then replace each if needed.(or "reptlace all" in case with ')
Then in first field again put '(or &,>,<,")
and in second write \'
then replace each if needed.(or "replace all" in case with ')
2. for other problematic symbols
I use to comment each next half part +rebuild
until i won't find the wrong sign.
E.g. an escaped word "\update" unexpectedly drops such error :)
To Find all the ' in your file, do the following:
Ctrl + F [Enter in the search field apostrophe] '
' should be written as "World's"

How to put a "-" in string.xml file

I need to be able to put a "-" in the string inside my strings.xml file.
My problem is that when I am putting my string which is "1261eba2-9d8c-11e1-93e3-40409e0f44a1", eclipse yells:
Multiple annotations found at this line:
- Replace "-" with an "en dash" character (–,
&;#8211;)
How can I fix this?
So, when you read the error message, your answer will be that
you have to replace - with –. Then it should work fine =)
http://en.wikipedia.org/wiki/Dash
The other answers are OK for when you want to display the string to the user. The user can't really tell the difference between a "real" dash and the unicode trickery.
But, if you really must have the dash (e.g. because that string is used as a password somewhere, or as a url key for an API) then you can just use this format:
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
<string name="EVA_API_KEY">3c42b725-5e20-41c8-982f-dee40be8a05b</string>
</resources>
The warning will be removed and the string can be read using the regular:
getResources().getString(R.string.EVA_API_KEY);
Use back slash ( \ ) in front of every special character. like me\&android.
This called escape character. ( \ )
For hyphen use (&#45) (-)...
<string name="abc">Welcome - Bro...</string>
and For more symbol use below link
http://www.degraeve.com/reference/specialcharacters.php
Enjoy...
The dash is a punctuation mark that is similar to a hyphen or minus sign, but differs from both of these symbols primarily in length and function. The most common versions of the dash are the en dash (–) and the em dash (—), named for the length of a typeface's lower-case n and upper-case M respectively.
Reference
Just replace - with – because when you type a dash on the keyboard, XML reads dash as minus, that's all.
You probably have this:
<string name="test1">1261eba2-9d8c-11e1-93e3-40409e0f44a1</string>
But you need either one of these:
<string name="test2">1261eba2–9d8c–11e1–93e3–40409e0f44a1</string>
<string name="test3">1261eba2–9d8c–11e1–93e3–40409e0f44a1</string>
In the second one the - is replaced by a –. It's hard to tell the difference visually.
The quick fix shortcut in Eclipse is Ctrl + 1 by default and in Android Studio is Alt + Enter by default.

Error in strings.xml file in Android

I have declared a long string in string.xml of an application.
Declared like this
<string name="terms">PLEASE READ THESE TERMS OF USE CAREFULLY BY ACCESSING THIS .................</string>
But this gives the following error :
error: Apostrophe not preceded by \ (in PLEASE READ THESE TERMS OF USE CAREFULLY
post your complete string. Though, my guess is there is an apostrophe (') character in your string. replace it with (\') and it will fix the issue. for example,
//strings.xml
<string name="terms">
Hey Mr. Android, are you stuck? Here, I\'ll clear a path for you.
</string>
Ref:
http://www.mrexcel.com/forum/showthread.php?t=195353
https://code.google.com/archive/p/replicaisland/issues/48
Solution
Apostrophes in the strings.xml should be written as
\'
Example
In my case I had an error with this string in my strings.xml and I fixed it.
<item>Most arguments can be ended with three words, "I don\'t care".</item>
Here you see my app builds properly with that code.
Here is the actual string in my app.
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Escaping apostrophes and quotes
If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don't work:
<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>
You have to put \ before an apostrophe. Like this \' , Also check that you are editing strings.xml and not values.xml (android studio directs you to this file when shows the error). Because if you edit values.xml and try to compile again, the error persists. This was happening to me recently.
Use this regex (?<!\\)' for searching an unescaped apostrophe.
It finds an apostrophe that not preceded by a backslash.
I use hebrew(RTL language) in strings.xml.
I have manually searched the string.xml for this char: '
than I added the escape char \ infront of it (now it looks like \' ) and still got the same error!
I searched again for the char ' and I replaced the char ' with \'(eng writing) , since it shows a right to left it looks like that '\ in the strings.xml !!
Problem solved.
The best answer of simply escaping the ' with \' works but I do not like the solution because when using replace all with ' to \', it only works once. The second time you'll be left with \\'.
Therefore, use: \u0027.
You may be able to use unicode equivalent both apostrophe and other characters which are not supported in xml string. Apostrophe's equivalent is "\u0027" .
1. for error: unescaped apostrophe in string
what I found is that AAPT2 tool points to wrong row in xml, sometimes.
So you should correct whole strings.xml file
In Android Studio, in problem file use :
Edit->find->replace
Then write in first field \' (or \&,>,\<,\")
in second put '(or &,>,<,")
then replace each if needed.(or "reptlace all" in case with ')
Then in first field again put '(or &,>,<,")
and in second write \'
then replace each if needed.(or "replace all" in case with ')
2. for other problematic symbols
I use to comment each next half part +rebuild
until i won't find the wrong sign.
E.g. an escaped word "\update" unexpectedly drops such error :)
To Find all the ' in your file, do the following:
Ctrl + F [Enter in the search field apostrophe] '
' should be written as "World's"

Categories

Resources