How do I take a string and convert it to lower or upper case in Ruby?
相关问题
- how to split a list into a given number of sub-lis
- Generate string from integer with arbitrary base i
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
相关文章
- JSP String formatting Truncate
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- Selecting only the first few characters in a strin
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
Since Ruby 2.4 there is a built in full Unicode case mapping. Source: https://stackoverflow.com/a/38016153/888294. See Ruby 2.4.0 documentation for details: https://ruby-doc.org/core-2.4.0/String.html#method-i-downcase
The ruby
downcase
method returns a string with its uppercase letters replaced by lowercase letters.https://ruby-doc.org/core-2.1.0/String.html#method-i-downcase
Like @endeR mentioned, if internationalization is a concern, the unicode_utils gem is more than adequate.
String manipulations in Ruby 2.4 are now unicode-sensitive.
The Rails Active Support gem provides
upcase
,downcase
,swapcase
,capitalize
, etc. methods with internationalization support:You can find out all the methods available on a String by opening irb and running:
And for a list of the methods available for strings in particular:
I use this to find out new and interesting things about objects which I might not otherwise have known existed.
Ruby has a few methods for changing the case of strings. To convert to lowercase, use
downcase
:Similarly,
upcase
capitalizes every letter andcapitalize
capitalizes the first letter of the string but lowercases the rest:If you want to modify a string in place, you can add an exclamation point to any of those methods:
Refer to the documentation for String for more information.