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?
解决方法
为了比较这些字符串,您需要对它们进行标准化,以便它们对这些类型的字符使用相同的字节序列.
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.
原文地址:https://www.jb51.cc/ruby/270950.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。