{

Ruby How to convert a string into upper and lower case?


This tutorial explains about how to convert a String into upper case or lower case

How to Convert a string into Lower case and uppercase in Ruby?

Ruby provides following methods

  • downcase

  • upcase

  • upcase: Convert the string into UPPER CASE

Below example, convert the string into upper case and assign the value to variable.

result="welcome".upcase
puts "#{result}"

Or Below example uses exclamation symbol(!) to the method that modifies the variable which is equal to convert the string int uppercase and assign the variable.

result="welcome"
result.upcase!
puts "#{result}"

The below program outputs the same result

WELCOME
  • downcase: Convert the string into lower or small case

Below example, convert the string into small case and assign the value to variable.

result="Welcome".downcase
puts "#{result}"

Or Below example uses exclamation symbol(!) to the method that modifies the variable which is equal to convert the string int lowercase and assign the variable.

result="Welcome"
result.downcase!
puts "#{result}"

The below program outputs the same result

welcome

There is another method swapcase that converts the UPPER CASE to lower case , and lower to upper case swapcase method that converts each character in a string to reverseable case. For example if letter is UPPERCASE, Convert to small case, If it is Small case, Convert to Upper case

result="welcome".swapcase 
puts "#{result}"
result1="Welcome".swapcase 
puts "#{result1}"
result2="WELCOME".swapcase 
puts "#{result2}"

Output:

WELCOME
wELCOME
welcome
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to install and uninstall gem in Ruby Programming| Ruby on Rails by Example

How to do sum of array numbers in Ruby with examples

How to check the type of a variable is Ruby| Ruby on Rails By Example

How to convert Class or Hash Object to JSON Object Ruby| Ruby on Rails By Example

How to remove duplicate elements from an array in Ruby with examples

How to Convert current Unix timestamp epoch to DateTime in Ruby Programming| Ruby on Rails by Example

How to remove an element from an array in Ruby Programming| Ruby on Rails by Example