WEBVTT

00:01.450 --> 00:07.930
Hello again! In this video, we are going to look at basic string operations. So this section is

00:07.930 --> 00:12.190
going to look at the C++ library standard string class.

00:12.880 --> 00:13.930
It's a little bit unusual.

00:13.930 --> 00:17.950
It doesn't quite fit in with the other containers in C++.

00:18.640 --> 00:25.630
The reason for that is that the string class was originally designed in isolation based on various classes

00:25.630 --> 00:28.450
that people had hacked up, because there wasn't one in C++.

00:29.740 --> 00:34.300
And then later on, the standard template library came along and they decided it would be a good idea

00:34.720 --> 00:41.350
to make the interface for the standard string consistent with the interface for containers in the standard

00:41.350 --> 00:42.190
template library.

00:42.610 --> 00:47.140
So the string actually has two interfaces: its own native interface and the STL container.

00:47.770 --> 00:50.950
So the result of this is that it has a very big interface.

00:51.550 --> 00:55.690
I think there's over 100 member functions, and don not worry, we are not going to cover them all!

00:56.440 --> 00:59.920
But we are going to look at some of the most important and the most useful ones.

01:00.430 --> 01:04.210
So in this video, we are going to start off with the basic string operations.

01:06.610 --> 01:09.330
So, some of the things you can do with strings. You can assign them.

01:09.340 --> 01:12.550
So you can say that one string is equal to another string.

01:13.570 --> 01:19.060
You can append strings so you can add one string onto the back of another string.

01:20.470 --> 01:26.740
You can concatenate strings, which means that you take one string and another string and then return

01:28.030 --> 01:30.490
the first string, followed by the second string, without modifying them.

01:31.090 --> 01:32.920
And you can also do a comparison.

01:33.250 --> 01:39.730
You can see whether one string is less than another, or equal to, or greater than equal to, and so on.

01:41.500 --> 01:48.190
Obviously, these all work with standard strings, but they can also work with C-style strings on the

01:48.190 --> 01:49.270
right hand side as well.

01:50.680 --> 01:56.200
So you can assign the string to a C-style string, you can append and so on.

01:58.730 --> 02:03.620
Concatenation works if either of the strings is a C-style string, because you are not modifying them.

02:03.620 --> 02:05.000
So that is pretty straightforward.

02:05.780 --> 02:10.370
Modifying a C-style string is generally a bit of a pain, especially if it is on the stack.

02:11.810 --> 02:14.240
And these - except for assignment.

02:14.540 --> 02:20.060
So these three operations also work if there is a char on the right hand side. So you can append a single

02:20.060 --> 02:24.980
character to a string, you can compare a string against a single character and so on.

02:25.670 --> 02:29.750
And you can also have a single character for either one of these arguments in a concatenation.

02:33.630 --> 02:39.360
Talking of C-style strings, there is a lot of codes around that still uses C-style strings.

02:39.360 --> 02:45.300
It's either interfaces in C, to perhaps an operating system or a database or some third party library.

02:45.870 --> 02:49.260
Or old code, or code that has been written in an old way.

02:50.250 --> 02:54.750
And the good news is that you don't have to use C-style strings in your code all [over] the place.

02:54.990 --> 02:58.590
You can actually convert a standard string to a C-style string.

02:59.280 --> 03:06.360
There is a member function called c underscore str, and this will return a copy of the data in the string

03:07.140 --> 03:08.910
as a C-style string.

03:08.910 --> 03:13.920
So that's an array of const char with a null character terminating it.

03:15.610 --> 03:18.630
And this will actually return a pointer to the const char.

03:19.330 --> 03:24.280
So this is useful for working with code that is designed to take C-style strings.

03:27.690 --> 03:31.140
And the last operation we are going to look at is getting a substring.

03:31.470 --> 03:33.840
So that is just the substr member function.

03:34.590 --> 03:38.400
The argument is the index where you want to start the substring.

03:38.790 --> 03:39.720
So if we have

03:41.410 --> 03:47.260
argument six, that means the element with index six, six and in "Hello world", that's going to be the

03:47.360 --> 03:47.680
'w'.

03:48.400 --> 03:52.030
So this will return everything from that element up to the end of the string.

03:52.840 --> 03:54.250
So that is going to return "world".

03:56.650 --> 04:01.420
We can all so put a second argument, which is just the number of characters in the substring.

04:01.960 --> 04:05.890
So in this case, we're going to have the same substring, but only get the first two characters.

04:06.550 --> 04:08.440
So that is going to be the 'w' and the 'o'.

04:14.050 --> 04:15.670
So let's try this out.

04:16.150 --> 04:18.700
We have got a standard string, "Hello world".

04:20.140 --> 04:20.560
We are

04:21.580 --> 04:23.670
practicing our use of index notation.

04:23.740 --> 04:27.740
So we are going to change the second element to an 'a'. So it is now going to be

04:27.760 --> 04:28.720
"Hallo world".

04:30.150 --> 04:34.500
And then we are going to get the sub strings that we had in the slide and then we are going to print all these

04:34.500 --> 04:35.400
strings out.

04:36.970 --> 04:38.200
So what do we get?

04:40.920 --> 04:41.780
So we get

04:41.910 --> 04:42.270
"Hallo

04:42.300 --> 04:47.790
world", because we changed the second element. We get the substring from index six, which is "world".

04:48.450 --> 04:54.450
And then we get the same substring, but only the first two characters, which is "wo". And then

04:54.450 --> 04:58.050
finally, constructors, which perhaps I should have mentioned before.

04:58.050 --> 05:00.090
But there are ones which take substring.

05:00.090 --> 05:03.930
So... I mean, it is difficult to get started in C++.

05:03.930 --> 05:07.020
You cannot really do everything step by step.

05:07.020 --> 05:11.340
You have to sort of approach from the air and circle around and get some idea.

05:11.340 --> 05:14.130
Then you can go in and land, and look at things more closely.

05:16.200 --> 05:19.320
The default constructor, which takes no arguments.

05:20.530 --> 05:22.920
That will create an empty string with no data in it.

05:23.760 --> 05:30.300
So zero length. We can construct a string from a string literal. As we have been doing so far, without

05:30.900 --> 05:32.010
really mentioning it.

05:33.780 --> 05:38.250
There is another form which takes a number and a character.

05:38.820 --> 05:45.210
So this first number is the number of elements, and this character is the value of the elements.

05:45.690 --> 05:52.230
So three and 'x'x is going to give us a string with three elements which are 'x'. So we get the string

05:52.230 --> 05:52.900
"xxx".

05:54.570 --> 05:58.380
We can also use an initializer list, which is possibly not that useful.

05:59.840 --> 06:01.940
And we can also use a substring.

06:02.180 --> 06:05.600
We do not actually need to call the substring member function directly.

06:06.020 --> 06:08.720
We can just give the string and the index.

06:09.470 --> 06:12.650
So this will get the substring starting with index one,

06:12.680 --> 06:14.240
so that is going to be "ello".

06:15.530 --> 06:19.610
And if we add another argument, that will be the number of elements in the substring.

06:20.300 --> 06:23.870
So that is going to be the first three characters, which is just "ell".

06:28.150 --> 06:30.460
So this is all coded up here.

06:30.670 --> 06:35.290
We are also going to print out the number of characters in each string and then the data.

06:35.770 --> 06:36.850
So cout, left

06:36.850 --> 06:42.910
shift string will just print out the data in the string. As we have been doing, again without really saying.

06:45.030 --> 06:49.860
So we have our default constructor with an empty string, so expect zero length and no data.

06:50.990 --> 06:58.310
C-style string, the repeated value, the initializer list and then the two versions with substrings.

07:00.740 --> 07:04.310
And there we are, we get an empty string with nothing in it.

07:06.260 --> 07:09.770
And the others are all what we would expect from the slide.

07:11.150 --> 07:13.100
Okay, so that is it for this video.

07:13.580 --> 07:14.420
I'll see you next time.

07:14.420 --> 07:17.570
But meanwhile, keep coding!
