看似相同的字符串比较失败 [英] 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));
在控制台中,我有:
false
'1Ä4±'=='1Ä4±'
string
string
我猜我的字符串包含奇怪的字符,那么我应该如何比较它们呢?我阅读了比较unicode字符时Javascript字符串比较失败,但在我的情况下是 x
和 y
来自同一来源,并且具有相同的编码.
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.
推荐答案
您字符串中的Ä
可以表示为单个UNICODE字符(复合字符,其中包含拉丁文大写字母A(U + 0041)后跟组合透析(U + 0308)变音符号.
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.
也可能有任意数量的零宽度空格(U +200B),以及字符串中的其他不可见"字符.
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.
这篇关于看似相同的字符串比较失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!