3

I want to check if a string contains the restricted symbol (®). As of now, I'm doing it like this:

if(mystr.contains("®"))
{
    //do stuff
}

This seems to work, but I really don't think using the restricted symbol in actual code is the best approach here. What is an alternative way to check if a string contains the restricted symbol?

jros
  • 714
  • 1
  • 10
  • 33
  • 1
    What about looking up the Unicode value and then use `\u` and then the number (no space in between)? – 11684 Oct 01 '15 at 19:15
  • yeah I agree. you want a generic solution. this might work on one machine not another – Caffeinated Oct 01 '15 at 19:16
  • 2
    What's wrong with using ® in the string literal? Java has supported UTF-8 source files for years. – James M Oct 01 '15 at 19:17
  • @JamesMcLaughlin I feel that using a character that is not on my keyboard is a bad idea, regardless of whether or not it will actually cause any issues. Seeing the little restricted symbol in the code would cause me some confusion if I didn't know what was going on, especially since in the code it's often too small to tell what it really is. – jros Oct 02 '15 at 16:03

1 Answers1

5

With the right encoding there shouldn't be a problem with your code. But you can also escape it to avoid issues due to the encoding used by the editor:

if(mystr.contains("\u00AE"))

During the lexical translation phase, the compiler will translate to the Unicode character.

See also Why does Java permit escaped unicode characters in the source code?

Community
  • 1
  • 1
M A
  • 71,713
  • 13
  • 134
  • 174