1213

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Heat Miser
  • 19,438
  • 7
  • 35
  • 38

11 Answers11

1755

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
  • 4
    Watch out! looks to me like using the bang "!" will return nil if there's no capital letter. so str = "this".downcase! returns str = nil – Phil_Ken_Sebben Nov 02 '12 at 22:29
  • 16
    Oftentimes, "bang methods" return `nil`; you should use them if you want to change an object in place, not if you want to store the value in another variable. – Sophie Alpert Nov 02 '12 at 23:28
  • but it'is a problem in "i" char if you are using utf-8. For instance, string = FEN BİLİMLERİ. string.capitalize must be "Fen bİlİmlerİ" or it could be changed because of css font style choice. – eayurt Jan 03 '13 at 12:01
  • `'coração'.upcase` produces `'CORAçãO'`. It may be advised to use some gem like “unicode_utils“, “activesupport“ or “Unicode”. – Fernando Basso Apr 24 '16 at 22:55
128

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.

mlambie
  • 7,467
  • 6
  • 34
  • 41
  • 12
    The only problem with this answer is that #own_methods doesn't appear to exist. Is it from an Irb extension? – Mark Wilden Feb 01 '12 at 23:38
  • 12
    Hi - I thought I was learning something new with the #own_methods then, but it doesn't exist for me either. However, I usually go: ("MyString".methods - Object.merhods).sort – oceanician May 09 '12 at 17:04
  • Ditto. @mlambie might have something like [this monkey patch](http://www.wikyblog.com/AmanKing/A_Ruby_object%27s_own_methods) set up somewhere. – fakeleft Dec 04 '12 at 21:09
  • 2
    Very similar to the patch @fakeleft referenced, and I have it in my .irbrc file. I monkey patch Object and create #own_methds with this: (obj.methods - obj.class.superclass.instance_methods).sort – mlambie Feb 03 '13 at 18:34
  • What's the difference between 'all the methods available on a String' and 'the methods available for strings in particular' ? They seem synonymous. Of course it doesn't particular matter since own_methods seems to be some extension I don't have, but I still am curious. – Laser Jan 30 '15 at 08:25
  • 1
    @Laser The methods for String in particular are the ones defined in the String class itself. The methods available on a String include the ones defined in its superclass(es). – Nic May 15 '15 at 00:47
  • @QPaysTaxes Got it, thanks. Any idea why own_methods doesn't exist for me in irb ruby 2.1.4? Are there no methods by default in the String class, so it would just be if I manually had added some? And if I wanted to, do you know where I'd find basic ruby classes, like String, in a rails app? Thanks – Laser May 16 '15 at 20:49
  • @Laser Well, according to [fakeleft's comment](http://stackoverflow.com/questions/1020568/how-to-convert-a-string-to-lower-or-upper-case-in-ruby/1021299?noredirect=1#comment18832728_1021299), it's possible a monkey patch or something. It's not a real extension. I have no idea where you'd find the definitions in a Rails installation; presumably in the same place as a Ruby one. The problem is that it might be in C, so unless you can read C you wouldn't be able to read the source file. – Nic May 16 '15 at 20:51
  • 1
    Use [`String.public_instance_methods(false)`](https://ruby-doc.org/core-2.6.5/Module.html#method-i-public_instance_methods) to find all public instance methods specifically defined by `String`. – 3limin4t0r Nov 15 '19 at 22:41
  • Or you can look for official documentation, all methods which belong to any class are already over there. :) – SEGV Dec 26 '21 at 19:48
41

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
nurettin
  • 11,090
  • 5
  • 65
  • 85
22

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

Toma Nistor
  • 786
  • 10
  • 18
Heat Miser
  • 19,438
  • 7
  • 35
  • 38
  • ruby conversions here: http://www.techotopia.com/index.php/Ruby_String_Conversions – TStamper Jun 20 '09 at 00:20
  • 2
    While technically this does help answer the question, it really should be more illuminating. Show the result perhaps? Maybe a link to the documentation? – the Tin Man Mar 09 '16 at 23:50
15

... and the uppercase is:

"Awesome String".upcase
=> "AWESOME STRING"
mlambie
  • 7,467
  • 6
  • 34
  • 41
14

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  ÁÂÃÀÇÉÊÍÓÔÕÚ"
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
helder.vasc
  • 847
  • 8
  • 8
8

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

'TESTING'.swapcase #=> testing
'testing'.swapcase #=> TESTING
  • 2
    Thanks! This is exactly what I was looking for. A mixed case string might make it more obvious exactly what this method does... `'Testing'.swapcase #=> tESTING` – Matthew May 29 '19 at 19:38
2

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

"hii".downcase
"hii".titleize
"hii".upcase
Foram
  • 483
  • 5
  • 12
1

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

mmichaa
  • 760
  • 6
  • 11
1

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.

Mason SB
  • 445
  • 1
  • 3
  • 13
0

In combination with try method, to support nil value:

'string'.try(:upcase)
'string'.try(:capitalize)
'string'.try(:titleize)
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40