- Read Tutorial
- Watch Guide Video
In this lesson we 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:
"Astros".upcase
Now if I always a string to be in lower case letters I can use the downcase
method, like so:
"Astros".downcase
Those are both 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
And lastly if you want to reverse the order of the letters in the string we can call the reverse
method:
"Astros".reverse
These methods are built into the String
data class and we can all them on any string values in Ruby.
Method Chaining
Another neat thing we can do is join different methods together to get custom output. For example, I can run:
"Astros".reverse.upcase
and this displays the value SORTSA
.
This practice of combining different methods with a dot is called method chaining.
A Practical Implementation
Now, you may be wondering why it's helpful to manipulate strings. It's cool to see, but is there any practical value? Let me walk you through a real world case study to illustrate the importance of string manipulation.
Whenever I'm building a search engine inside an application that I'm working on there are many times where I need to convert the case of either the value being searched for, or the value from the database. For example, if a user wants to search for the word Milk
, its highly likely that my database query will not return the associated record or information because it does not handle case sensitivity well by default. If I have the word milk
in my database it wouldn't be returned because database searches are case sensitive. On the other hand, if I convert the entire word to an uppercase or lowercase value, depending on how the database is setup, the database query will work properly and return the correct results. This is one example the importance of string manipulation in programming.
We've only touched the basics when it comes to string manipulation. Please reference the String documentation to see the full list of methods.
Give it a Bang!
Before we end this guide I want to discuss something you may have noticed when it comes to Ruby methods. There are a number of methods that have the similar names, with the only difference being that one method will have a !
and the other does not.
devCamp note: the !
is called bang
in Ruby. So if you see a method such as my_method!
, out loud you'd say "my_method bang".
Let's take a look at an example of how bang
is used for the upcase
method. The main difference between the two methods is that the bang
version will permanently change the value of the variable that the string was stored in.
In general, when !
is used at the end of a method, it means the original value or string is being changed.