WEBVTT

00:01.230 --> 00:05.670
Hello again! In this video, we are going to look at removing elements from strings.

00:07.020 --> 00:09.570
The erase function does what you might expect.

00:09.990 --> 00:14.460
It will remove a character or characters from a string object.

00:15.570 --> 00:21.060
We call it on the string that we want to erase from. The first argument is the position where we want

00:21.060 --> 00:22.200
to start erasing.

00:22.710 --> 00:25.320
And the second document is the number of characters.

00:26.370 --> 00:30.210
So here we have three, which is the second letter 'l'.

00:30.600 --> 00:33.510
And one means we just wants to erase one character.

00:34.110 --> 00:35.580
So this will actually start erasing

00:35.580 --> 00:41.340
at that character, not before it or after it, actually at that character, and it will just

00:41.340 --> 00:42.330
erase one character.

00:42.990 --> 00:44.210
So that will be that letter 'o'.

00:45.660 --> 00:48.390
And again, we can use the return value from find.

00:49.380 --> 00:55.700
So we are going to find the first occurrence of the letter 'e' and then erase two characters, so that will erase

00:55.770 --> 00:56.630
'e' and 'l'.

00:59.190 --> 01:06.120
So here is that code. We erase the second letter 'l', then we erase 'e' and 'l'. So we should be just left

01:06.120 --> 01:07.260
with "ho".

01:09.170 --> 01:09.740
And there we are.

01:10.910 --> 01:16.880
So it is not quite Christmas yet, but maybe it is topical to have ho. Ho, ho ho? Maybe not!

01:20.840 --> 01:23.300
We can also use erase with iterators.

01:24.200 --> 01:29.270
If we just use a single iterator, it will erase the corresponding element.

01:29.690 --> 01:35.840
So if we have an iterator to the first element, by calling begin(), that will erase the first element.

01:36.650 --> 01:42.560
If we have a range of iterators, that will erase all the elements in the range up to, but not including,

01:42.560 --> 01:43.940
the end of the range.

01:44.600 --> 01:47.450
So that is the half open range that we mentioned before.

01:48.470 --> 01:54.350
If we have, for example, an iterator to the second element and the last element, this will erase

01:54.350 --> 02:00.350
all the elements from the second element up to, but not including, the last element.

02:03.150 --> 02:04.650
And we can try that out.

02:05.100 --> 02:11.700
So you remember that end() minus one is the actual last element. And end() on its own is the last element

02:11.700 --> 02:12.210
plus one.

02:14.330 --> 02:15.200
So what do we get?

02:16.250 --> 02:19.840
So we erase the first element and we get "allo".

02:21.820 --> 02:26.890
And then we erase everything except the first and last element of that string.

02:27.130 --> 02:28.480
So we end up with "eo".

02:33.720 --> 02:38.430
If we want to remove some characters from a string and replace them with other characters, then we can

02:38.430 --> 02:39.330
use replace().

02:42.450 --> 02:47.640
The first argument to replace is the position where we want to start replacing.

02:48.970 --> 02:52.300
The second argument is the number of characters we want to replace.

02:52.930 --> 02:58.390
So we are starting from the letter 'H' and replacing five characters, and then we are going to replace

02:58.390 --> 03:01.180
them with the third argument, which is "Goodbye".

03:06.640 --> 03:10.900
And of course, being careful programmers, we check that we have actually got a valid index before we use

03:10.900 --> 03:11.140
it.

03:11.410 --> 03:12.880
So we put in this check here.

03:15.150 --> 03:18.180
And then we should end up with a string that says, "Say Goodbye".

03:19.930 --> 03:22.090
So there it is. We started with "Say Hello".

03:22.360 --> 03:24.400
Then we replaced "Hello" with "Goodbye".

03:26.550 --> 03:28.950
And again, we can use iterators.

03:32.560 --> 03:38.710
So we have a range of iterators, so we say that, everything in this half-open range, we want to

03:38.710 --> 03:39.190
replace.

03:39.760 --> 03:44.980
So in this case, we started with the first element, then everything up to, but not including, first

03:44.980 --> 03:46.120
element plus three.

03:46.810 --> 03:47.950
So that is the fourth element.

03:48.310 --> 03:50.290
So we are replacing everything before that.

03:51.190 --> 03:56.500
So we are replacing the first three elements, and then we are replacing them with "Wave".

04:01.290 --> 04:02.250
So there we are.

04:02.580 --> 04:05.820
We are going to replace the first three elements with "Wave".

04:09.470 --> 04:13.010
So we started out with "Say Goodbye", and now we are saying "Wave Goodbye".

04:18.780 --> 04:23.370
And finally, if we want to replace all the characters in the string with something else, we can do

04:23.450 --> 04:26.040
it in a single operation with "assign".

04:26.910 --> 04:30.870
So we just put the argument that we want to replace all the characters with.

04:33.440 --> 04:36.830
So we have "Hello", and we are going to replace all the characters with "Goodbye".

04:38.390 --> 04:39.170
And there we are.

04:40.280 --> 04:42.040
Okay, so that's it for this video.

04:42.050 --> 04:43.310
It's time for me to "say goodbye"!

04:43.820 --> 04:44.930
I'll see you again next time.

04:44.930 --> 04:47.180
But meanwhile, keep coding!
