Any task you need to be done with strings can be done with the tools we have learned so far: .size(), .find(), and .substr(). But if you check out a reference for the <string> library, you will find many other functions. These other functions can make some jobs easier than trying to do the work with just the three member functions we have learned about. This section introduces a few of them but does not cover all of the functions.
size_t rfind(string s, size_t pos = npos) works just like find, but searches in reverse (from right to left). You can give it a position to start from, otherwise it starts from the end of the string. Say we want to find the last / in a string. We could write a loop that looks for /, then keeps looking for another / after that location until it canβt find any more. rfind() makes this much easier:
find_first_of and find_last_of let you specify a set of characters to look for. Say we want to find the first vowel in a string. One way would be to write our own loop that checks letter by letter until it find something that when made lowercase matches a e i o or u.:
// assume string s is defined
size_t firstVowelLocation = string::npos; // assume no vowel
for (int i = 0; i < s.size(); ++i) {
// get current letter, make it lower case, cast result to char
char c = static_cast<char>( tolower( s.at(i) ) );
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
firstVowelLocation = i;
break; //end loop
}
}
But find_first_of can handle the loop for us. We just need to tell it what to look for (any upper or lower case letter).
The other class of string member functions it is worth knowing about are string modifying functions. These are ones that change an existing string (instead of making a copy of part of it like substr does). The two key ones are erase and replace.
You need to tell it what index to start erasing from and optionally, how many characters to erase (everything from that location to end is the default. Here it is in action:
To do the same work without erase, we would need to grab two substrings, concatenate them, and store the result back into a variable. Below is an example of what that looks like (you can test it in the activecode above if you like.)
string message = "Hello there!";
string part1 = message.substr(0, 5); //first 5 characters
string part2 = message.substr(11); //everything at or after index 11
// assign message a new value based on combining part1 and part2
message = part1 + part2;
replace looks similar to erase, but has an additional parameter str that is put where the erased text was. Here is what it would look like to erase the word World (5 charcters) that starts at index 6, with the string "you":
Again, we could do something equivalent without replace by grabbing the substrs before and after the part we want to replace and then concatenating them with something like: message = part1 + "you" + part2;
We have a string s that might be an email address with an @. Write code to detect if it has an @. If so, remove everything from the @ symbol on. If not, print an error message.