Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seemingly identical strings fail comparison

I am encountering a strange issue while 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 guess my strings contain strange characters, so how should I compare them? I read Javascript string comparison fails when comparing unicode characters but in my case, x and y come from the same source and have the same encoding.

like image 325
little-dude Avatar asked May 28 '13 19:05

little-dude


1 Answers

The Ä in your strings can be represented either as a single UNICODE character (Latin Capital Letter A With Diaeresis, U+00C4), or as a composite character consisting of Latin Capital Letter A (U+0041) followed by a Combining Diaeresis (U+0308) diacritic.

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

Therefore, both strings may render the same, but actually be different.

like image 196
Frédéric Hamidi Avatar answered Oct 21 '22 13:10

Frédéric Hamidi