90

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?

2
  • 1
    where exactly do you need that value? Is it a tag, a tag value or an attribute value?
    – rekaszeru
    May 14, 2012 at 6:49
  • 2
    replace "-" with "\-"
    – Carmen
    Feb 10, 2014 at 14:08

7 Answers 7

124

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

3
  • 4
    This is OK when you just want to display the string to the user. The user can't really tell that you are "cheating" with a similar looking unicode character. If you just want to use the regular dash see this answer: stackoverflow.com/a/10895509/78234
    – Tal Weiss
    Mar 26, 2014 at 15:09
  • 2
    I think that code is for endash, not a regular hyphen.
    – John61590
    Sep 14, 2016 at 17:19
  • Ya that's an en-dash not a hyphen thepunctuationguide.com/en-dash.html
    – Blundell
    Jun 25, 2020 at 11:39
88

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);
3
  • 10
    One tip: you might want to put the tools:ignore="TypographyDashes" part into just those <string> tags that really need the real dash.
    – Dan J
    Feb 25, 2013 at 6:25
  • 1
    For some reason it only worked for me when I put the ignore on the string i'm using. Thanks for the answer I found out it was the wrong dash, but how to get it to work wasn't as straightforward. Jun 17, 2014 at 8:49
  • 1
    I was trying to launch the phone dialer for 9-1-1 and your answer was the key for that. Thanks!
    – Roisgoen
    Mar 27, 2015 at 21:41
15

Use back slash ( \ ) in front of every special character. like me\&android.

This called escape character. ( \ )

4
  • I think you mean add the escape character: "\" before every special character.
    – drew212
    May 14, 2012 at 6:51
  • en.wikipedia.org/wiki/Escape_character escape character is backslash - "\" not forward slash - "/"
    – drew212
    May 14, 2012 at 6:53
  • check my answer now. this is back slash (). May 14, 2012 at 7:02
  • 7
    when im using what you all are saing its removing the "-" char... what im getting is: 1261eba29d8c11e193e340409e0f44a1 insted of 1261eba2-9d8c-11e1-93e3-40409e0f44a1...
    – roiberg
    May 14, 2012 at 7:05
5

For hyphen use (&#45) (-)...

<string name="abc">Welcome &#45; Bro...</string>

and For more symbol use below link

http://www.degraeve.com/reference/specialcharacters.php

Enjoy...

4

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.

1

You probably have this:

<string name="test1">1261eba2-9d8c-11e1-93e3-40409e0f44a1</string>

But you need either one of these:

<string name="test2">1261eba2&#8211;9d8c&#8211;11e1&#8211;93e3&#8211;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.

2
  • 1
    Be careful though: if you change these symbols in a URL it might stop working (mine did).
    – Dan J
    Feb 25, 2013 at 6:22
  • I want to replace a short dash symbol (n-dash) with an escaped sequence. But &#8211; looks like its the escape sequence for the long dash symbol (m-dash). Jun 10, 2016 at 18:52
0

The quick fix shortcut in Eclipse is Ctrl + 1 by default and in Android Studio is Alt + Enter by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.