I am using Netbeans to write a java software. As part of debugging my code, I got stuck in the following issue. I have a code that compares two arabic letters that appear to be exactly the same. I have copied the letters from my database. I expected the code to find them equal but it doesn't. Below is my code:
String a = "و";
String b ="و";
System.out.println(a.equals(b));
Note: When I write the letter using the keyboard I get an output of "true", but when I copy it from the database to the variables "a" and "b". The output is false. Why?
Comparing Unicode characters is not overly hard, but is more involved than what you are doing.
You have copied and pasted two glyphs. Glyphs are graphical representations of codepoints. Sometimes the glyphs are similar (or identical) when the codepoints are different. In such a case, you will fail the .equals(...)
test when it appears it should have passed.
This can happen for a number of reasons. A frequent culprit is that your character is "composed". To support some systems, a character might actually be composed of a primary codepoint, with an additional codepoint modifying it. (Think of a common latin letter with an accent that is encoded as a second code point). Unicode supports this, but it also has a codepoint for the same combination combined as one character.
For other languages, there may be other similar rules, which make the codepoint comparison of .equals(...)
fail when the glyph representation appears to be identical.