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

RubyStringUppercaseLowercase

Ruby Problem Overview


How do I take a string and convert it to lower or upper case in Ruby?

Ruby Solutions


Solution 1 - 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.

Solution 2 - Ruby

You can find out all the methods available on a String by opening irb and running:

"MyString".methods.sort

And for a list of the methods available for strings in particular:

"MyString".own_methods.sort

I use this to find out new and interesting things about objects which I might not otherwise have known existed.

Solution 3 - Ruby

Like @endeR mentioned, if internationalization is a concern, the unicode_utils gem is more than adequate.

$ gem install unicode_utils
$ irb
> require 'unicode_utils'
=> true
> UnicodeUtils.downcase("FEN BİLİMLERİ", :tr)
=> "fen bilimleri"

String manipulations in Ruby 2.4 are now unicode-sensitive.

Solution 4 - Ruby

The ruby downcase method returns a string with its uppercase letters replaced by lowercase letters.

"string".downcase

https://ruby-doc.org/core-2.1.0/String.html#method-i-downcase

Solution 5 - Ruby

... and the uppercase is:

"Awesome String".upcase
=> "AWESOME STRING"

Solution 6 - Ruby

The Rails Active Support gem provides upcase, downcase, swapcase,capitalize, etc. methods with internationalization support:

gem install activesupport
irb -ractive_support/core_ext/string
"STRING  ÁÂÃÀÇÉÊÍÓÔÕÚ".mb_chars.downcase.to_s
 => "string  áâãàçéêíóôõú"
"string  áâãàçéêíóôõú".mb_chars.upcase.to_s
=> "STRING  ÁÂÃÀÇÉÊÍÓÔÕÚ"

Solution 7 - Ruby

The .swapcase method transforms the uppercase latters in a string to lowercase and the lowercase letters to uppercase.

'TESTING'.swapcase #=> testing
'testing'.swapcase #=> TESTING

Solution 8 - Ruby

You can find strings method like "strings".methods You can define string as upcase, downcase, titleize. For Example,

"hii".downcase
"hii".titleize
"hii".upcase

Solution 9 - Ruby

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

Solution 10 - Ruby

Won't work for every, but this just saved me a bunch of time. I just had the problem with a CSV returning "TRUE or "FALSE" so I just added VALUE.to_s.downcase == "true" which will return the boolean true if the value is "TRUE" and false if the value is "FALSE", but will still work for the boolean true and false.

Solution 11 - Ruby

In combination with try method, to support nil value:

'string'.try(:upcase)
'string'.try(:capitalize)
'string'.try(:titleize)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHeat MiserView Question on Stackoverflow
Solution 1 - RubySophie AlpertView Answer on Stackoverflow
Solution 2 - RubymlambieView Answer on Stackoverflow
Solution 3 - RubynurettinView Answer on Stackoverflow
Solution 4 - RubyHeat MiserView Answer on Stackoverflow
Solution 5 - RubymlambieView Answer on Stackoverflow
Solution 6 - Rubyhelder.vascView Answer on Stackoverflow
Solution 7 - RubyRajkumar UlaganadhanView Answer on Stackoverflow
Solution 8 - RubyForamView Answer on Stackoverflow
Solution 9 - RubymmichaaView Answer on Stackoverflow
Solution 10 - RubyMason SBView Answer on Stackoverflow
Solution 11 - RubyElvack RiansyahView Answer on Stackoverflow