Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

characters
string
ruby
communitycreator

How to change characters of a string to uppercase permanently

okwudili onyejiaku

Overview

Converting the characters in a string to uppercase is easy to do in Ruby. We can achieve this with the upcase method. But this won’t modify the string permanently. However, with the upcase! method, we can permanently change lowercase characters present in a string to their uppercase equivalent.

Syntax

str.upcase!

Parameters

str: this is the string whose characters we want to permanently change to uppercase

Return Value

A new string is returned which is the uppercase of characters present in string str. However, if all or any of the characters are uppercase, nothing will be changed.

Code Example

# create some strings
str1 = "edpresso"
str2 = "IS"
str3 = "grEAt!"

# to uppercase
str1.upcase!
str2.upcase!
str3.upcase!

# print strings
puts str1
puts str2
puts str3
Convert lowercase characters to uppercase permanently in Ruby

Explanation

  • Lines 2-4: we created some strings whose characters we want to convert to uppercase permanently.
  • Lines 7-9: the upcase! method was called on the strings created.
  • Lines 12-14: the results were printed to the console.

As you can see, the characters in the strings were permanently changed to uppercase characters.

RELATED TAGS

characters
string
ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring