Invisible characters in Java strings

String a = "Hello\u200e"; String b = "Hello\u200f"; System.out.println("a = '" + a + "' and b = '" + b + "' are length " + a.length() + " and " + b.length() + ", equals() is " + a.equals(b)); 

The code in the above code snippet produces the following output.

a = 'Hello' and b = 'Hello' - lengths 6 and 6, equals () - false

Although the value of both a and b displayed on the console is Hello‏ a.equals(b) returns false . How?

+7
source share
2 answers

U+200E and U+200F not for printing. They are both control characters that determine how text should be displayed - either from left to right, or from right to left.

You will not see them in the terminal, and they should not be equivalent lines.

0x200E ^ 0x200F != 0

+9
source

Because character sequences are not identical. Just because it looks the same on the console does not mean that the objects are identical.

+9
source

All Articles