English

Learn Ruby – How To Convert a String to Lower or Uppercase?

 
In Ruby, a String contains and manipulates an arbitrary sequence of bytes, typically representing characters. When you want to change the contents of a String in Ruby, it should be noted that methods with the name ending in “!” modify the String that is receiving this method, while those without “!” return a new String.
 

How to convert a string to Lower or Uppercase of a String in Ruby

To convert the content to lowercase, use .downcase:

"Hello James!".downcase
"hello james!"

Similarly, to transform everything to uppercase, use .upcase:

"Hello James!".upcase
"HELLO JAMES!"

If you want to capitalize only the first letter of a String, use the .capitalize method:

"hello james!".capitalize
"Hello james!"

You can also use .titleize to ensure that the first letter of each word in the String will be uppercase:

"hello james" .titleize
"Hello James"

Remember that if you add the exclamation point in any of these methods, the original String will be modified, as shown below:

string ="Hello James!"
string.downcase!
string 
"Hello james!"

 

Including support for Unicode, and ASCII in capitalization methods in Ruby

These methods operate natively in ASCII. To add support for Unicode, is necessary to install a gem, as showed below:

$ Gem install unicode_utils
irb
> require 'Unicode_utils'
=> true
> UnicodeUtils.downcase ("FEN BİLİMLERİ",: Tr)
=> "Fen bilimleri"

Do you know other ways to perform this case changes on a String in Ruby? Share your comments in the section below!

If you want to explore other questions about this programming language, you can check out our Ruby programming videos about Ruby. Below we show some examples:

You can also subscribe to some channels that broadcast programming in Ruby, such as the following:

 j-mcnally

 

 

josephwilk

 

 

Another cool way to find out interesting things about Ruby is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

3 weeks ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 month ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

1 month ago

Binance Auto Invest – the Best Innovation in Crypto since Sliced Bread

At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…

2 months ago

Highest Stable Coin Yields – March 2024

As we kick off another week, it's time to explore the top yield platforms for…

2 months ago

Education Ecosystem Featured on Business Insider

We're excited to share that Education Ecosystem was recently featured in an article on Business…

2 months ago