Upcase, downcase String ExamplesUppercase and lowercase a string with the upcase and downcase methods. Change a string in-place.
This page was last reviewed on Jun 4, 2021.
Upcase, downcase. The upcase() and downcase() methods affect letters. Upcase changes lowercase letters to uppercase. And downcase changes uppercase letters to lowercase.
Notes, assignment. You must assign to the result of upcase() and downcase(). The original string instance is not modified—a modified copy of the string is made.
First example. We begin by calling the upcase method, and then invoking the downcase method on the result. When we call these methods, only letters are affected.
Info Numbers and spaces are not affected or removed by upcase and downcase—they are left alone in the string.
# An input string. value = "BLUE bird" # Change cases. upper = value.upcase() lower = value.downcase() # Display results. puts upper puts lower
BLUE BIRD blue bird
In-place. Suppose we want to modify a string in-place, without having to assign the result of upcase or downcase. We can use an exclamation mark to modify the current string variable.
test = "bird" # Use exclamation mark to modify the string in-place. test.upcase! puts test
A summary. In Ruby, we sometimes have logic that preprocesses files containing data that may have inconsistent cases. Often we want to lowercase (or uppercase) every string we encounter.
File, IO
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 4, 2021 (image).
© 2007-2023 Sam Allen.