Hello again! In this video, we are going to look at removing elements from strings.
The erase function does what you might expect.
It will remove a character or characters from a string object.
We call it on the string that we want to erase from. The first argument is the position where we want
to start erasing.
And the second document is the number of characters.
So here we have three, which is the second letter 'l'.
And one means we just wants to erase one character.
So this will actually start erasing
at that character, not before it or after it, actually at that character, and it will just
erase one character.
So that will be that letter 'o'.
And again, we can use the return value from find.
So we are going to find the first occurrence of the letter 'e' and then erase two characters, so that will erase
'e' and 'l'.
So here is that code. We erase the second letter 'l', then we erase 'e' and 'l'. So we should be just left
with "ho".
And there we are.
So it is not quite Christmas yet, but maybe it is topical to have ho. Ho, ho ho? Maybe not!
We can also use erase with iterators.
If we just use a single iterator, it will erase the corresponding element.
So if we have an iterator to the first element, by calling begin(), that will erase the first element.
If we have a range of iterators, that will erase all the elements in the range up to, but not including,
the end of the range.
So that is the half open range that we mentioned before.
If we have, for example, an iterator to the second element and the last element, this will erase
all the elements from the second element up to, but not including, the last element.
And we can try that out.
So you remember that end() minus one is the actual last element. And end() on its own is the last element
plus one.
So what do we get?
So we erase the first element and we get "allo".
And then we erase everything except the first and last element of that string.
So we end up with "eo".
If we want to remove some characters from a string and replace them with other characters, then we can
use replace().
The first argument to replace is the position where we want to start replacing.
The second argument is the number of characters we want to replace.
So we are starting from the letter 'H' and replacing five characters, and then we are going to replace
them with the third argument, which is "Goodbye".
And of course, being careful programmers, we check that we have actually got a valid index before we use
it.
So we put in this check here.
And then we should end up with a string that says, "Say Goodbye".
So there it is. We started with "Say Hello".
Then we replaced "Hello" with "Goodbye".
And again, we can use iterators.
So we have a range of iterators, so we say that, everything in this half-open range, we want to
replace.
So in this case, we started with the first element, then everything up to, but not including, first
element plus three.
So that is the fourth element.
So we are replacing everything before that.
So we are replacing the first three elements, and then we are replacing them with "Wave".
So there we are.
We are going to replace the first three elements with "Wave".
So we started out with "Say Goodbye", and now we are saying "Wave Goodbye".
And finally, if we want to replace all the characters in the string with something else, we can do
it in a single operation with "assign".
So we just put the argument that we want to replace all the characters with.
So we have "Hello", and we are going to replace all the characters with "Goodbye".
And there we are.
Okay, so that's it for this video.
It's time for me to "say goodbye"!
I'll see you again next time.
But meanwhile, keep coding!