In this section, we'll walk through a practical string method that enables you to substitute string values that I use in many real-world applications:
str = "The quick brown fox jumped over the quick dog"
If you notice, I've modified this sentence a bit to have the quick word appear twice in my sentence.
Now, if I want to substitute it with the slow word, I can use the sub method (which is short for substitution):
str = "The quick brown fox jumped over the quick dog"
str.sub "quick", "slow"
In the preceding code, I'm calling the sub method on my str string variable and passing two arguments to the sub method:
- The first is the word the program has to find
- The second is the replacement word
If I run this code, my output will be "The slow brown fox jumped over the quick dog":
If you notice...