WEBVTT

00:02.000 --> 00:08.840
Hello again! In this video, we're going to look at string literals. The traditional way to get a string

00:08.840 --> 00:12.920
literal is to take some characters and put them inside a pair of double quotes.

00:13.430 --> 00:16.610
So here we have a string literal, "hello world".

00:17.540 --> 00:19.760
This is a C-style string literal.

00:20.150 --> 00:23.930
It's an array of char which is terminated by a null character.

00:24.920 --> 00:28.050
The problem with this is that you can't really do very much with it.

00:28.340 --> 00:29.810
You can iterate over the characters.

00:30.350 --> 00:33.500
You can print them out and you can access them as a pointer.

00:35.090 --> 00:40.100
One thing that I've often wanted to do with string literals is to take two string literals and add them

00:40.100 --> 00:43.910
together to give a single string literal with all the characters from both strings.

00:43.910 --> 00:47.200
But you can't do that, or at least not directly.

00:48.320 --> 00:53.030
And if you want to do something with it, it's an array, so it's only compatible with things that are

00:53.030 --> 00:54.680
expecting an array of that length.

00:57.300 --> 01:05.070
We mentioned in the last video that you can create your own suffixes for your own objects. In C++ 14,

01:05.340 --> 01:10.440
they actually implemented that for the string type in the library, so you can now have a suffix which

01:10.440 --> 01:12.390
returns a standard string object.

01:13.230 --> 01:18.510
So this hello string here is actually an object of type standard string, and you can do anything

01:18.510 --> 01:20.370
with that, that you can do with a string object.

01:20.910 --> 01:26.070
So you can concatenate them in just a normal string operation and you can pass it to anything which

01:26.070 --> 01:27.450
expects a string object.

01:28.620 --> 01:32.550
This is implemented in its own namespace, so to be able to get this to work,

01:32.550 --> 01:36.510
we need to put using namespace std literals.

01:39.830 --> 01:45.440
So we start off by creating a C-style string literal, then a standard string literal.

01:45.800 --> 01:49.160
We need to include the literals namespace for that to work.

01:50.210 --> 01:53.390
And then finally, we prove that we can do string operations.

01:55.900 --> 01:56.710
So there we are.

01:56.980 --> 02:01.770
And they all display, "hello, world", but in the first case, it's an array and in the other two cases,

02:01.780 --> 02:03.100
it's a string object.

02:04.480 --> 02:08.530
And just to prove that this really is a string and not

02:10.240 --> 02:15.940
something that's a const character, so we've now converted these back to C-style arrays, and you

02:15.940 --> 02:22.110
can't add arrays in C++, so that an error. Cannot add two pointers.

02:23.650 --> 02:24.100
Okay.

02:27.170 --> 02:30.260
In a string literal, some characters have a special meaning.

02:30.500 --> 02:36.650
For example, if we put backslash n, that means it's a new line character, so that's called an escape.

02:37.760 --> 02:42.200
The backslash is called the escape character, so it changes the meaning of the next character.

02:42.500 --> 02:43.650
It means it's not the letter n,

02:44.090 --> 02:45.530
it's a new line character.

02:46.850 --> 02:52.490
Similarly, if we want to put a double quote inside a string literal, we can't just put a double quote on its own

02:52.490 --> 02:54.080
because that would mean the end of the string.

02:54.950 --> 03:00.410
If we put a backslash quote, that means we just want the double quote character to be one of the characters

03:00.410 --> 03:01.580
inside the string literal.

03:03.970 --> 03:08.330
But this can be a bit problematical if you have a string literal with lots of escaped

03:08.440 --> 03:10.540
characters in, or characters that need escaping.

03:10.900 --> 03:16.140
So something like this we have quotation marks, and a backslash, and a backslash.

03:16.360 --> 03:20.890
So we don't want this to be a backslash less than, because there is no such character.

03:21.490 --> 03:23.890
We want this to be a real backslash inside the string.

03:24.610 --> 03:26.530
So to do that, we need a double backslash.

03:29.960 --> 03:35.090
And we also need to put a backslash in front of the quotation marks. Otherwise, the compiler will think

03:35.090 --> 03:36.050
that's the end of the string.

03:36.920 --> 03:40.280
So if you want a string which appears like this, we need to write it as this.

03:40.280 --> 03:48.080
So backslash quote backslash quote, then backslash to the backslash, then backslash quote and then, well,

03:48.170 --> 03:49.370
lots and lots of backslashes.

03:49.700 --> 03:50.540
It's a bit of a mess.

03:51.550 --> 03:54.670
It's not quite clear what this is going to look like.

03:54.680 --> 03:57.260
And also, this is quite right? Three back slashes?

03:58.070 --> 03:59.660
So this is not a great way to do things.

04:04.680 --> 04:10.950
In C++ 11, you can now have a raw string, so you write the string exactly as you want it to appear.

04:11.760 --> 04:19.570
And then you put capital R, double quote, open bracket before the string and then close bracket,

04:19.620 --> 04:21.180
double quote at the end of the string.

04:21.480 --> 04:24.060
So this will turn this into a raw string.

04:28.100 --> 04:33.890
And this means that the double quote and the backslash characters aren't going to be processed

04:33.920 --> 04:36.980
as escaped characters, they'll just be taken literally, if you like :)

04:38.990 --> 04:44.390
So if we run this program and print out this string, then it'll be exactly like that. What happens

04:44.390 --> 04:48.200
if the string contains a bracket, a closing bracket, followed by double quote?

04:48.830 --> 04:50.750
In that case, we add a delimiter.

04:51.380 --> 04:55.850
So we have this which has x86, then closing bracket, then double quote.

04:56.390 --> 04:58.400
So we don't want that to end the raw string.

04:59.240 --> 05:04.730
So what we do is we put this delimiter character x after the double quote, and that means that we need

05:04.820 --> 05:09.380
to have a closing bracket x double quote, before we actually finish off the string.

05:09.590 --> 05:15.590
So this close bracket quote here doesn't end the string. This close bracket x does end the string.

05:16.890 --> 05:17.140
Right.

05:17.910 --> 05:18.510
I hope you got that!

05:21.880 --> 05:27.820
Say here's three different ways of writing the string. So we have the traditional way with lots of back

05:27.820 --> 05:28.360
slashes.

05:30.880 --> 05:37.990
And then we have a raw string, which includes the close bracket, close quote, so we add the delimiter

05:38.000 --> 05:41.560
to make sure that the string doesn't stop there, it goes all the way to the end.

05:42.820 --> 05:44.020
So let's see what we get.

05:46.620 --> 05:47.760
So all of these...

05:50.340 --> 05:54.360
So all of these print out the URL as we want it to appear.

05:54.720 --> 05:59.340
The first one, we had to put all these back slashes in. The second one, we used the raw string.

05:59.850 --> 06:04.230
And then the third one, we had to use the delimiter to make sure we got the entire string and not just

06:04.650 --> 06:05.430
the bit up to there.

06:07.940 --> 06:08.360
Okay.

06:08.930 --> 06:10.610
So that's it for this video.

06:11.210 --> 06:15.200
I'll see you next time, but meanwhile, keep coding!
