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

ID : 1111

viewed : 228

Tags : rubystringuppercaselowercaseruby

Top 5 Answer for How to convert a string to lower or upper case in Ruby

vote vote


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.

vote vote


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.

vote vote


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.

vote vote


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


vote vote


... and the uppercase is:

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

Top 3 video Explaining How to convert a string to lower or upper case in Ruby