Adam Piotrowski
Adam Piotrowski

Reputation: 704

Comparing utf-8 string that have different byte in special characters

SomeObject.find(20).client.full_name.downcase.bytes.each{|b| puts b}

=> "błażejewsk" OtherClass.find(36).client.downcase.bytes.each {|b| puts b}

=> "bŁaŻejewsk"

As you can see special characters have different bytes in both string . Both string have Encoding:UTF-8, so force_encoding or something like that , wouldn't help. == returns false. How should I convert those string so I can get true from comparison?

Upvotes: 0

Views: 235

Answers (1)

poussma
poussma

Reputation: 7301

It will not work with downcase see http://www.ruby-doc.org/core-2.1.0/String.html#method-i-downcase-21

Returns a copy of str with all uppercase letters replaced with their lowercase counterparts. The operation is locale insensitive—only characters “A” to “Z” are affected. Note: case replacement is effective only in ASCII region.

See this question to properly downcase UTF-8 strings Ruby 1.9: how can I properly upcase & downcase multibyte strings?

Upvotes: 1

Related Questions