Seemingly identical strings fail comparison

前端 未结 4 581
星月不相逢
星月不相逢 2021-01-18 01:50

I am encountering a strange issue while comparing two strings. Here is my code:

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


        
相关标签:
4条回答
  • 2021-01-18 02:15

    Try to escape your two strings to see what chars are in them. In this case (although Frédéric has covered possible cases) since you're using PGP, you probably have a binary non-printable char present.

    escape(x);
    escape(y);
    

    in your console and you will be able to detect the char in action.

    0 讨论(0)
  • 2021-01-18 02:20

    If you are trying to do it in c# this might have to do something with Normalization. FormC vs FormD vs FormKC vs FormKD Reference : http://sharepoint.asia/two-exactly-same-strings-fail-while-comparison-in-c-net/

    0 讨论(0)
  • 2021-01-18 02:21

    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.

    0 讨论(0)
  • 2021-01-18 02:35

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

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

    prints "false" :)

    Comparing strings means comparing character codes. In some fonts, different character codes have the same "picture", like "l" and "I" (first is L, second is i). In my example above, first A is cyrillic, second is latin.

    0 讨论(0)
提交回复
热议问题