In this video, we'll learn about c++ strings.
Standard string is a class in the c++ standard template library or stl.
We could do an entire course on just the scl, and that course
would be very long and complex.
So in this video, I'll only talk about the major elements
of the c++ string class.
In order to use c++ plus strings, you must include the string header file.
Strings are in the standard namespace.
So in order to use them without using namespace standard, you
must prefix them with standard and the scope resolution operator.
This is also true for the standard string methods
that work with c++ strings.
Like c-style strings, c++ strings are stored contiguously in memory.
However, unlike c-style strings which are fixed in size, c++
strings are dynamic and can grow and shrink as needed at runtime.
C++ strings work with the stream insertion and extraction operators
just like most other types in c++.
The c++ string class provides a rich set of methods or functions that
allow us to manipulate strings easily.
Chances are that if you need to do something with the string
that functionality is already there for you without having
to rewrite it from scratch.
C++ strings also work with most of the operators that we're used to for
assigning, comparing and so forth.
This is a huge advantage over c-style strings since c-style strings don't
work well with those operators.
Even though c++ strings are preferred in most cases sometimes
you need to use c-style strings.
Maybe you're interfacing with a library that's been
optimized for c-style strings.
Well, in this use case, you can still use c++ strings
and take advantage of them.
And when you need to you can easily convert the c++ string into
a c-style string and back again.
Like vectors, c++ strings are safer since they provide methods
that can bounce check and allow you to find errors in your code
so you can fix them before your program goes into production.
Let's see how we can declare and initialize c++ strings.
In all the examples in this video, I'm assuming that the string header
file has been included and that we're using the standard namespace.
Here you can see six examples of declaration and
initialization of c++ strings.
There are other ways as well using constructor and assignment syntax.
But I'm mainly using the initializer syntax in this video.
In the first example, we declare S1 as a string.
Notice that the string type is lowercase.
Unlike c-style strings, c++ strings are always initialized.
In this case, S1 is initialized to an empty string.
No garbage and memory to have to worry about.
In the second example, I'm declaring and initializing
S2 to the string Frank.
Notice that frank is a c-style literal, that's okay.
It will be converted to a c++ string.
In the third example, S3 is initialized from S2, so
a copy of S2 is created.
S2 and S3 will both be Frank, but different Franks in
different areas of memory.
In the fourth example, I'm declaring and initializing S4 from Frank.
But I'm only using the first three characters of the string Frank.
So S4 will be initialized to the string fra.
In the fifth example, I'm initializing S5 from S3 which is Frank.
But notice the two integers that follow the S3 and the initializer.
The first integer is the starting index and the second is the length.
So in this case, we initialize S5 with the two characters
that index 0 and 1 from S3.
So S5 will be initialized to fr.
Finally, we can initialize the string to a specific number
of a specific character.
In this case, three x's.
Note that this uses constructor syntax with the parentheses
instead of the curlies.
Now that we've declared some strings, let's see how we can
assign other values to them.
With c++ strings, you can use the assignment operator.
This feels much more natural than having to use the stream
copy function like we would have to in c-style strings.
In this example, I've declared S1 and it's empty.
Then I can assign the c-style literal c++ rocks to S1.
Pretty cool and pretty easy.
S1 will grow dynamically as needed.
In the second example, I've declared S2 and initialized it to hello.
Then I assign S1 to S2.
In this case, S2 will no longer contain hello.
It will contain a copy of S1, c++ rocks.
Let's see how we can concatenate strings together.
Concatenation of strings just means building up a string
from two other strings.
We can use the plus operator to concatenate c++ strings.
In this example, I created two strings part one which is c++
and part two which is powerful.
Then I have an empty string sentence.
Notice that I'm assigning two sentence the concatenated result
of part one plus a space plus part two plus a space plus language.
If I displayed sentence now, it would display c++ is a powerful language.
Notice that the last example on the slide will not compile.
This is because we have two c-style literals.
And you can't concatenate c-style literals.
It only works for c++ strings.
A combination of c++ strings and c-style strings is okay though as
we saw in the previous assignments.
Just like we did with vectors, we can use the same operators
to access string elements.
In this case, the elements of a string are characters.
So we can use the subscript operator as well as the at method.
Remember, the app method performs bounce checking.
So if you go over bounds, you'll get an exception which you can fix.
Let's see how we can display screen characters one at a time.
In this example, we have a string S1 initialized to Frank.
We can use the range based for loop to display the string characters.
In this case, f-r-a-n-k and the null character will be displayed.
Pretty much what you expected, right.
Notice that the type of the loop variable is char in this case.
What do you think will happen if we change that to integer.
In this case, I've changed it to integer.
Is this what you expected.
We told the compiler to use an integer and that's exactly what it's doing.
So instead of displaying the character value of each element in the string,
it's displaying the integer value that represents those characters.
So in this case, 70 114 97 110 107 and 0 which represent f r a n k
and of course the null character.
These are the ascii codes for those characters.
Comparing c++ strings couldn't be any easier or more intuitive.
We use the same equality and relational operators that
we've been using all along.
We're comparing two string objects, so they'll be compared character
by character, and their character values will be compared lexically.
So a capital a is less than a capital z, and a capital a
is less than a lowercase a.
That's because the capital letters come before the lowercase
letters in the ascii table.
We can't use these operators on two c-style literals, but we can
use them in the following cases.
If we have two c++ strings, if we have one c++ string and a c-style
literal or if we have one c++ string and one c-style string.
Let's see some examples.
Here we're defining five c++ string variables, S1 through S5.
And then we perform some comparison operations and see the results.
Of course, you would normally use these Boolean expressions
in an if statement or looping conditional expressions.
In the first example, we check to see if S1 is equal to S5.
This is true since they both contain the string apple.
S1 equals S2 is false since S1 is apple and S2 is banana.
How about S1 not equal to S2.
This is true since apple is not equal to banana.
In the case of S1 less than S2, this is also true since apple comes before
banana lexically in the ascii table.
S2 greater than S1 is also true since banana comes before apple lexically.
Notice that banana has an uppercase b whereas apple has a lowercase a.
S4 less than S5 is false since apple with a lowercase does not come
before apple with an upper case.
And then finally, A1 equal apple is true because they're the same.
Notice in this case, apple is a c-style string literal.
The c++ string class has a rich set of very useful methods, too many methods
to cover in detail in this video.
I encourage you to study the c++ string class since it's going
to be a class that you'll use often, and it's important that
you know what it provides, so you don't reinvent the wheel
when you need to solve a problem.
The substring method extracts a substring from a c++ string.
It doesn't change the string.
It simply returns the substring and you could do whatever you want with it.
In this case, I'm simply displaying it.
But you can easily assign it to a string variable.
Here, I've initialized S1 to this is a string.
The first example takes a substring of this string starting at index 0
and including exactly 4 characters.
If there are less than 4 characters left in the original string then all
the remaining characters are included.
In this case, the substring is the first word in the string, this.
In the second example, we return the substring starting at index
five and include two characters.
That's the substring is, IS.
Finally the last example starts at index 10 and includes four characters,
this will return the substring test.
Let's see how we can search a string for another.
The c++ string class has a very handy method named find.
Find works with characters and strings.
It expects a string or character and returns the index or position
of the beginning of that string or character in the original string.
So if we have a string S1 that's initialized to this is a test and we
want to find the string this, we'd get back a 0 since this starts at index 0.
In the second example we're looking for the string is.
In this case, it would return 2 since the first is starts at index 2.
In the third example, we're finding the string test, and we get back a 10.
In the fourth example, we're searching for a single character, the lowercase e,
which is found at index 11.
In the fifth example, we use a variant of the method that
also allows the index where you want to start the search from.
In this case, I want to find the is substring again.
But I want to start at index 4.
So this time it finds the is that's located at index 5.
Finally, what happens if the string or character we want
to find just isn't there.
Well, in this case the method returns an end position, which means no
position information available.
You can check for this value in an if statement.
And if true, you know what you were searching for wasn't there.
Very easy, very powerful.
There's also an r find method that starts searching from the end of the
string to the beginning of the string.
We can also remove characters from a c++ string using the
erase and clear methods.
For the erase method, you provide the starting index and
how many characters to delete.
The clear method deletes all the characters in the string so the
string becomes the empty string.
We've seen a lot of string methods and you can see
how powerful this class is.
Let's look at one more useful method and one more useful
operator that are commonly used.
The method is the length method.
It returns the number of characters currently in the string object.
In this example, S1 is Frank.
So s1.length will return a 5.
This is so easy and something that's impossible to do with c
cell strings since they don't contain size information.
The operator i wanted to cover is the compound concatenation
assignment operator.
In this case, S1 is Frank.
And I can say S1 plus equals James.
And James will be concatenated to Frank and the entire result
string will be assigned back to S1.
This is really handy and works very much the same way that the compound
assignment operators worked with integers and doubles and so forth.
There are also many more methods in the c++ string class for you
to discover as you study c++.
Okay, there's one more thing I'd like to talk about before we end this video,
input with c++ strings.
C++ strings work great with input and output streams.
As you've seen, inserting c++ string variables to an output stream like
cout is pretty easy and works just like we've been doing all along.
Extracting a c++ string from an input stream like cin also
works the same way we expect.
However, there's one issue that's also true for c-style strings.
Suppose we've defined S1 as a c++ string and we extract
a string from cin as usual.
Now suppose I type in hello space there.
When I display S1, I will only see hello.
The there was not extracted.
This is because the extraction operator stops
when it sees whitespace.
In many cases, we want to read an entire line of text up to
when the user presses enter.
For example, I want the string to be hello there.
Suppose I asked you to enter your full name.
I want to be able to read William Smith, not just William.
In this case, we can use the getlined function.
The getline function has a couple of variants.
The first variant expects two elements inside the parentheses.
The first element is the input stream.
In this case, we're using cin which defaults to the keyboard.
The second element is the name of the c++ string where you want the
text that the user enters stored.
That's it. Very easy.
In the example, I'm saying getline cin S1.
Now everything the user types is stored into S1.
Getline stops reading when it sees the new line.
It doesn't include the new line in the string it just discards it.
The other variant of getline has another element in the parentheses.
The first two are the same as before, the input stream and
the c++ string variable name.
The third is called the delimiter.
This is the character that you want getline to stop reading input at.
So as long as the user doesn't enter this character, everything will
be stored in the string variable.
Once the delimiter is seen, it's not included in the string
variable and it's discarded.
In the last example, I'm using a lowercase x as the delimiter.
So if i type this is x, then the string stored in S1 will be
this is and the x is discarded.
Well, we've covered a lot of material in this video, and there's much
more in the string class to learn.
But this gives you a good starting point so you can use
the c++ string class effectively.
Also you've now been introduced to object oriented programming
with both vectors and strings.
Pretty soon, we'll be developing our own classes which is pretty cool.
That completes this video.
Please play with the string class.
Create examples, assign, delete, display and try out some of
the methods in this video.
It won't take long before you're really comfortable
working with c++ strings.