Hello again! In this video, we're going to look at string literals. The traditional way to get a string
literal is to take some characters and put them inside a pair of double quotes.
So here we have a string literal, "hello world".
This is a C-style string literal.
It's an array of char which is terminated by a null character.
The problem with this is that you can't really do very much with it.
You can iterate over the characters.
You can print them out and you can access them as a pointer.
One thing that I've often wanted to do with string literals is to take two string literals and add them
together to give a single string literal with all the characters from both strings.
But you can't do that, or at least not directly.
And if you want to do something with it, it's an array, so it's only compatible with things that are
expecting an array of that length.
We mentioned in the last video that you can create your own suffixes for your own objects. In C++ 14,
they actually implemented that for the string type in the library, so you can now have a suffix which
returns a standard string object.
So this hello string here is actually an object of type standard string, and you can do anything
with that, that you can do with a string object.
So you can concatenate them in just a normal string operation and you can pass it to anything which
expects a string object.
This is implemented in its own namespace, so to be able to get this to work,
we need to put using namespace std literals.
So we start off by creating a C-style string literal, then a standard string literal.
We need to include the literals namespace for that to work.
And then finally, we prove that we can do string operations.
So there we are.
And they all display, "hello, world", but in the first case, it's an array and in the other two cases,
it's a string object.
And just to prove that this really is a string and not
something that's a const character, so we've now converted these back to C-style arrays, and you
can't add arrays in C++, so that an error. Cannot add two pointers.
Okay.
In a string literal, some characters have a special meaning.
For example, if we put backslash n, that means it's a new line character, so that's called an escape.
The backslash is called the escape character, so it changes the meaning of the next character.
It means it's not the letter n,
it's a new line character.
Similarly, if we want to put a double quote inside a string literal, we can't just put a double quote on its own
because that would mean the end of the string.
If we put a backslash quote, that means we just want the double quote character to be one of the characters
inside the string literal.
But this can be a bit problematical if you have a string literal with lots of escaped
characters in, or characters that need escaping.
So something like this we have quotation marks, and a backslash, and a backslash.
So we don't want this to be a backslash less than, because there is no such character.
We want this to be a real backslash inside the string.
So to do that, we need a double backslash.
And we also need to put a backslash in front of the quotation marks. Otherwise, the compiler will think
that's the end of the string.
So if you want a string which appears like this, we need to write it as this.
So backslash quote backslash quote, then backslash to the backslash, then backslash quote and then, well,
lots and lots of backslashes.
It's a bit of a mess.
It's not quite clear what this is going to look like.
And also, this is quite right? Three back slashes?
So this is not a great way to do things.
In C++ 11, you can now have a raw string, so you write the string exactly as you want it to appear.
And then you put capital R, double quote, open bracket before the string and then close bracket,
double quote at the end of the string.
So this will turn this into a raw string.
And this means that the double quote and the backslash characters aren't going to be processed
as escaped characters, they'll just be taken literally, if you like :)
So if we run this program and print out this string, then it'll be exactly like that. What happens
if the string contains a bracket, a closing bracket, followed by double quote?
In that case, we add a delimiter.
So we have this which has x86, then closing bracket, then double quote.
So we don't want that to end the raw string.
So what we do is we put this delimiter character x after the double quote, and that means that we need
to have a closing bracket x double quote, before we actually finish off the string.
So this close bracket quote here doesn't end the string. This close bracket x does end the string.
Right.
I hope you got that!
Say here's three different ways of writing the string. So we have the traditional way with lots of back
slashes.
And then we have a raw string, which includes the close bracket, close quote, so we add the delimiter
to make sure that the string doesn't stop there, it goes all the way to the end.
So let's see what we get.
So all of these...
So all of these print out the URL as we want it to appear.
The first one, we had to put all these back slashes in. The second one, we used the raw string.
And then the third one, we had to use the delimiter to make sure we got the entire string and not just
the bit up to there.
Okay.
So that's it for this video.
I'll see you next time, but meanwhile, keep coding!