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

741,037

Solution 1

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

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

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

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

... and the uppercase is:

"Awesome String".upcase
=> "AWESOME STRING"
Share:
741,037
Heat Miser
Author by

Heat Miser

I am a writer first, a freakish generalist programmer second, an amateur AI dweeb third, and a gadget nut last.

Updated on July 14, 2021

Comments

  • Heat Miser
    Heat Miser almost 3 years

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

  • a_h
    a_h almost 15 years
  • Heat Miser
    Heat Miser almost 15 years
    I only thought about it after I had answered it, but I'll give you the cred for taking the time to answer it anyway. Thanks!
  • Mark Wilden
    Mark Wilden about 12 years
    The only problem with this answer is that #own_methods doesn't appear to exist. Is it from an Irb extension?
  • oceanician
    oceanician almost 12 years
    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
  • Phil_Ken_Sebben
    Phil_Ken_Sebben over 11 years
    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
  • Sophie Alpert
    Sophie Alpert over 11 years
    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.
  • ap2
    ap2 over 11 years
    Ditto. @mlambie might have something like this monkey patch set up somewhere.
  • eayurt
    eayurt over 11 years
    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.
  • mlambie
    mlambie about 11 years
    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
  • Josh M.
    Josh M. about 10 years
    Of course, Ruby can't be like other languages and use the standard to upper or to lower! :)
  • CleoR
    CleoR over 9 years
    To upper and to lower sound like they operate on chars and not as a method on a string. Sounds very Cish to me.
  • Laser
    Laser over 9 years
    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.
  • Nic
    Nic almost 9 years
    @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).
  • Laser
    Laser almost 9 years
    @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
  • Nic
    Nic almost 9 years
    @Laser Well, according to fakeleft's comment, 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.
  • Graham S.
    Graham S. almost 9 years
    There is always .titleize, too, in case you want to capitalize every single word in a string. "gone with the wind".titleize #=> "Gone With The Wind". Just thought I would throw that in there.
  • PJP
    PJP about 8 years
    While technically this does help answer the question, it really should be more illuminating. Show the result perhaps? Maybe a link to the documentation?
  • Fernando Basso
    Fernando Basso about 8 years
    'coração'.upcase produces 'CORAçãO'. It may be advised to use some gem like “unicode_utils“, “activesupport“ or “Unicode”.
  • vikingsteve
    vikingsteve over 7 years
    And for an expression like this: #{node.environment} then the upcase goes inside the brackets like this: #{node.environment.upcase}
  • ronald8192
    ronald8192 about 7 years
    .titleize is from Rails. Cannot find it in ruby String documentation
  • Sagar Pandya
    Sagar Pandya about 6 years
    titleize needs to be removed from this answer or at least state it's a Rails method.
  • Matthew
    Matthew almost 5 years
    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
  • 3limin4t0r
    3limin4t0r over 4 years
    Use String.public_instance_methods(false) to find all public instance methods specifically defined by String.
  • Pablo
    Pablo almost 4 years
    Isn't #titleize RoR as opposed to pure Ruby?
  • SEGV
    SEGV over 2 years
    Or you can look for official documentation, all methods which belong to any class are already over there. :)