It seems that identical lines do not match

I am having a weird problem comparing two strings. Here is my code:

console.log(x == y); console.log("'" + x + "'=='" + y + "'"); console.log(typeof(x)); console.log(typeof(y)); 

In the console, I have:

 false '1Ä4±'=='1Ä4±' string string 

I think my lines contain strange characters, so how do I compare them? I read Javascript String Comparison failed when comparing Unicode characters , but in my case x and y come from the same source and have the same encoding.

+6
source share
4 answers

Ä in your lines, it can be represented as a single UNICODE character ( Latin Capital Letter A With Diaeresis, U + 00C4 ), or as a compound character consisting of Latin Capital Letter A (U + 0041) and then Combination of diaresis (U + 0308) diacritical.

There can also be any number of Zero-Width Spaces (U + 200B) , as well as other "invisible" characters in your lines.

Therefore, both lines can display the same thing, but actually be different.

+6
source

Try to avoid two lines to see what characters are in them. In this case (although Frederick considered possible cases), since you are using PGP, you probably have a binary non-printable char gift.

 escape(x); escape(y); 

in your console and you can detect char in action.

+2
source

BTW. try this code in JS (copy-paste) :)

 console.log("A" == ""); 

prints "false" :)

String comparison means comparing character codes. In some fonts, different character codes have the same "pattern", for example, "l" and "I" (first L, second - i). In my example above, the first A is Cyrillic, the second is Latin.

+1
source

If you are trying to do this in C #, maybe something needs to be done with normalization. FormC vs FormD vs FormKC vs FormKD Link: http://sharepoint.asia/two-exactly-same-strings-fail-while-comparison-in-c-net/

0
source

Source: https://habr.com/ru/post/946036/


All Articles