Hello again! In this video, we are going to look at basic string operations. So this section is
going to look at the C++ library standard string class.
It's a little bit unusual.
It doesn't quite fit in with the other containers in C++.
The reason for that is that the string class was originally designed in isolation based on various classes
that people had hacked up, because there wasn't one in C++.
And then later on, the standard template library came along and they decided it would be a good idea
to make the interface for the standard string consistent with the interface for containers in the standard
template library.
So the string actually has two interfaces: its own native interface and the STL container.
So the result of this is that it has a very big interface.
I think there's over 100 member functions, and don not worry, we are not going to cover them all!
But we are going to look at some of the most important and the most useful ones.
So in this video, we are going to start off with the basic string operations.
So, some of the things you can do with strings. You can assign them.
So you can say that one string is equal to another string.
You can append strings so you can add one string onto the back of another string.
You can concatenate strings, which means that you take one string and another string and then return
the first string, followed by the second string, without modifying them.
And you can also do a comparison.
You can see whether one string is less than another, or equal to, or greater than equal to, and so on.
Obviously, these all work with standard strings, but they can also work with C-style strings on the
right hand side as well.
So you can assign the string to a C-style string, you can append and so on.
Concatenation works if either of the strings is a C-style string, because you are not modifying them.
So that is pretty straightforward.
Modifying a C-style string is generally a bit of a pain, especially if it is on the stack.
And these - except for assignment.
So these three operations also work if there is a char on the right hand side. So you can append a single
character to a string, you can compare a string against a single character and so on.
And you can also have a single character for either one of these arguments in a concatenation.
Talking of C-style strings, there is a lot of codes around that still uses C-style strings.
It's either interfaces in C, to perhaps an operating system or a database or some third party library.
Or old code, or code that has been written in an old way.
And the good news is that you don't have to use C-style strings in your code all [over] the place.
You can actually convert a standard string to a C-style string.
There is a member function called c underscore str, and this will return a copy of the data in the string
as a C-style string.
So that's an array of const char with a null character terminating it.
And this will actually return a pointer to the const char.
So this is useful for working with code that is designed to take C-style strings.
And the last operation we are going to look at is getting a substring.
So that is just the substr member function.
The argument is the index where you want to start the substring.
So if we have
argument six, that means the element with index six, six and in "Hello world", that's going to be the
'w'.
So this will return everything from that element up to the end of the string.
So that is going to return "world".
We can all so put a second argument, which is just the number of characters in the substring.
So in this case, we're going to have the same substring, but only get the first two characters.
So that is going to be the 'w' and the 'o'.
So let's try this out.
We have got a standard string, "Hello world".
We are
practicing our use of index notation.
So we are going to change the second element to an 'a'. So it is now going to be
"Hallo world".
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
strings out.
So what do we get?
So we get
"Hallo
world", because we changed the second element. We get the substring from index six, which is "world".
And then we get the same substring, but only the first two characters, which is "wo". And then
finally, constructors, which perhaps I should have mentioned before.
But there are ones which take substring.
So... I mean, it is difficult to get started in C++.
You cannot really do everything step by step.
You have to sort of approach from the air and circle around and get some idea.
Then you can go in and land, and look at things more closely.
The default constructor, which takes no arguments.
That will create an empty string with no data in it.
So zero length. We can construct a string from a string literal. As we have been doing so far, without
really mentioning it.
There is another form which takes a number and a character.
So this first number is the number of elements, and this character is the value of the elements.
So three and 'x'x is going to give us a string with three elements which are 'x'. So we get the string
"xxx".
We can also use an initializer list, which is possibly not that useful.
And we can also use a substring.
We do not actually need to call the substring member function directly.
We can just give the string and the index.
So this will get the substring starting with index one,
so that is going to be "ello".
And if we add another argument, that will be the number of elements in the substring.
So that is going to be the first three characters, which is just "ell".
So this is all coded up here.
We are also going to print out the number of characters in each string and then the data.
So cout, left
shift string will just print out the data in the string. As we have been doing, again without really saying.
So we have our default constructor with an empty string, so expect zero length and no data.
C-style string, the repeated value, the initializer list and then the two versions with substrings.
And there we are, we get an empty string with nothing in it.
And the others are all what we would expect from the slide.
Okay, so that is it for this video.
I'll see you next time.
But meanwhile, keep coding!