我有两个字符串:
a = 'hà nội' b = 'hà nội'
当我将它们与== b进行比较时,它返回false.
我检查了字节码:
a.bytes = [104,97,204,128,32,110,195,180,163,105] b.bytes = [104,160,225,187,153,105]
原因是什么?如何修复它以使a == b返回true?
解决方法
这是
Unicode equivalence的问题.
为了比较这些字符串,您需要对它们进行标准化,以便它们对这些类型的字符使用相同的字节序列.
a.unicode_normalize == b.unicode_normalize
unicode_normalize(form =:nfc)[link]
Returns a normalized form of str,using Unicode normalizations NFC,
NFD,NFKC,or NFKD. The normalization form used is determined by form,
which is any of the four values :nfc,:nfd,:nfkc,or :nfkd. The
default is :nfc.If the string is not in a Unicode Encoding,then an Exception is raised. In this context,‘Unicode Encoding’ means any of UTF-8,UTF-16BE/LE,and UTF-32BE/LE,as well as GB18030,UCS_2BE,and UCS_4BE. Anything else than UTF-8 is implemented by converting to UTF-8,which makes it slower than UTF-8.