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
How to Understand Nil Vs. Empty Vs. Blank in Ruby
Difference Between "And" and && in Ruby
Difference Between Require_Relative and Require in Ruby
Getting Output of System() Calls in Ruby
How to Match All Occurrences of a Regex
Get the Name of the Currently Executing Method
How to Change Default Timezone For Active Record in Rails
Can't Install Ruby Under Lion With Rvm - Gcc Issues
Difference Between Attr_Accessor and Attr_Accessible
Where and How Is the _ (Underscore) Variable Specified
Rails 4: List of Available Datatypes
Ruby 1.9: Invalid Byte Sequence in Utf-8
How to Sort an Array in Descending Order in Ruby
How to Calculate Number of Chars Common to Two Strings