[ruby] How to convert a string to lower or upper case in Ruby

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

This question is related to ruby string uppercase lowercase

The answer is


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.


... and the uppercase is:

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

In combination with try method, to support nil value:

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

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  ÁÂÃÀÇÉÊÍÓÔÕÚ"

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 BILIMLERI", :tr)
=> "fen bilimleri"

String manipulations in Ruby 2.4 are now unicode-sensitive.


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

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

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.

"string".downcase

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


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

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

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.


Examples related to ruby

Uninitialized Constant MessagesController Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Ruby - ignore "exit" in code Empty brackets '[]' appearing when using .where find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite? How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? How to update Ruby with Homebrew?

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to uppercase

Capitalize the first letter of string in AngularJs Ignoring upper case and lower case in Java Convert from lowercase to uppercase all values in all character variables in dataframe In Android EditText, how to force writing uppercase? how to convert Lower case letters to upper case letters & and upper case letters to lower case letters How to convert a string from uppercase to lowercase in Bash? How to change a string into uppercase Java Program to test if a character is uppercase/lowercase/number/vowel How do I lowercase a string in Python? Capitalize or change case of an NSString in Objective-C

Examples related to lowercase

Ignoring upper case and lower case in Java how to convert Lower case letters to upper case letters & and upper case letters to lower case letters How to detect lowercase letters in Python? .toLowerCase not working, replacement function? How to convert a string from uppercase to lowercase in Bash? How to convert array values to lowercase in PHP? Java Program to test if a character is uppercase/lowercase/number/vowel How do I lowercase a string in Python? How do I lowercase a string in C? How to convert a string to lower case in Bash?