Ruby 1.9: How to Properly Upcase & Downcase Multibyte Strings

Ruby 1.9: how can I properly upcase & downcase multibyte strings?

Case conversion is locale dependent and doesn't always round-trip, which is why Ruby 1.9 doesn't cover it (see here and here)

The unicode-util gem should address your needs.

Rails: How to downcase non-English string?

str = "Привет"
str.mb_chars.downcase.to_s
#=> "привет"

How to convert a string to lower or upper case in Ruby

Ruby has a few methods for changing the case of strings. To convert to lowercase, use downcase:

"hello James!".downcase    #=> "hello james!"

Similarly, upcase capitalizes every letter and capitalize capitalizes the first letter of the string but lowercases the rest:

"hello James!".upcase      #=> "HELLO JAMES!"
"hello James!".capitalize #=> "Hello james!"
"hello James!".titleize #=> "Hello James!" (Rails/ActiveSupport only)

If you want to modify a string in place, you can add an exclamation point to any of those methods:

string = "hello James!"
string.downcase!
string #=> "hello james!"

Refer to the documentation for String for more information.

Rails: How to downcase non-English string?

str = "Привет"
str.mb_chars.downcase.to_s
#=> "привет"

Does Ruby share PHP's multibyte string problem?

I think Ruby 1.9 clears up this underlaying assumption

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

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?

Rails: How to downcase non-English string?

str = "Привет"
str.mb_chars.downcase.to_s
#=> "привет"


Related Topics



Leave a reply



Submit