String manipulation guide
In this section, you are going to learn about string manipulation, along with a number of examples of how to integrate string manipulation methods in a Ruby program.
What is string manipulation?
So what exactly is string manipulation? It's the process of altering the format or value of a string, usually by leveraging string methods.
String manipulation code examples
Let's start with an example. Let's say I want my application to always display the word Astros
in capital letters. To do that, I simply write this:
"Astros".upcase
Now if I always want a string to be in lower case letters, I can use the downcase
method, like this:
"Astros".downcase
These are the two methods I use quite often. However, there are other string methods available that we also have at our disposal.
For the rare times when you want to literally swap the case of the letters, you can leverage the swapcase
method:
"Astros".swapcase
Lastly, if you want to reverse the order of the letters in the string...