Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,840 --> 00:00:01,530
Welcome back.
2
00:00:01,710 --> 00:00:07,500
And this video I would like to teach you about stream manipulation, and that's going to be quite important
3
00:00:07,500 --> 00:00:11,700
because you are going to need it throughout your coding experience and career.
4
00:00:12,060 --> 00:00:17,130
So this is an extra lecture where we are going to talk about different ways of printing data on the
5
00:00:17,130 --> 00:00:17,730
screen.
6
00:00:17,880 --> 00:00:23,040
And we're going to look at composite strings, string formatting, string interpolation and verbatim
7
00:00:23,040 --> 00:00:23,570
strings.
8
00:00:23,790 --> 00:00:28,410
So, of course, you can stick to one way if you prefer one, but if you like all of them, then it's
9
00:00:28,410 --> 00:00:28,710
fine.
10
00:00:28,710 --> 00:00:32,430
But try to stick to one style throughout your project.
11
00:00:32,880 --> 00:00:35,610
So let's first of all define a few variables.
12
00:00:35,910 --> 00:00:40,160
So inside of my main method, I'm going to create two new variables.
13
00:00:40,170 --> 00:00:44,110
One is going to be H and then the name will be Alphonsus.
14
00:00:44,160 --> 00:00:46,440
So we have an integer, an age and the string name.
15
00:00:47,280 --> 00:00:53,820
Now let's first of all use string concatenation and I have covered this keyword before.
16
00:00:54,120 --> 00:00:55,440
Let's look at this real quick.
17
00:00:55,860 --> 00:01:01,500
So here we are using string concatenation where we say hello, my name is, and then the plus sign the
18
00:01:01,500 --> 00:01:08,880
variable plus and then another string, so to speak, plus another variable plus another string, so
19
00:01:08,880 --> 00:01:09,340
to speak.
20
00:01:09,600 --> 00:01:17,880
OK, so we have an in our an integer example where we basically said what kind of variables we are adding
21
00:01:17,880 --> 00:01:19,380
together in order to get a sum.
22
00:01:19,740 --> 00:01:21,570
So this is string concatenation.
23
00:01:21,870 --> 00:01:26,790
So let's run this and look at it and we will see it says String Concatenation.
24
00:01:26,820 --> 00:01:28,230
Hello, my name is Alfonso.
25
00:01:28,230 --> 00:01:29,570
I am 31 years old.
26
00:01:30,030 --> 00:01:35,390
So this works flawlessly with integers as well as with strings here.
27
00:01:35,640 --> 00:01:41,100
So this is a string and this is an integer and we are using it in string concatenation and there's no
28
00:01:41,100 --> 00:01:43,350
problem with it in Arkansas.
29
00:01:43,420 --> 00:01:43,680
Right.
30
00:01:43,680 --> 00:01:44,760
Line statement here.
31
00:01:45,360 --> 00:01:45,690
All right.
32
00:01:45,690 --> 00:01:49,860
Now let's look at the string formatting concept, OK?
33
00:01:49,860 --> 00:01:53,790
This is going to be a little more advanced, so let's look at it.
34
00:01:54,330 --> 00:01:57,120
So string formatting uses the index.
35
00:01:57,390 --> 00:02:01,850
OK, so here I have a statement that says that this is string formatting.
36
00:02:02,310 --> 00:02:05,040
Then we also use hello, my name is.
37
00:02:05,190 --> 00:02:13,890
But this time we don't end to string and use concatenation, but instead we're using curly brackets,
38
00:02:14,130 --> 00:02:18,650
then index zero for the first entry and index one for the second entry.
39
00:02:18,960 --> 00:02:26,940
So what we're saying is whatever is going to come after this comma here after my string add position
40
00:02:26,940 --> 00:02:27,440
zero.
41
00:02:27,450 --> 00:02:34,770
So the first entry will be positioned here and whatever is positioned, it's the index one, which is
42
00:02:34,770 --> 00:02:37,410
the second entry will be positioned here.
43
00:02:37,650 --> 00:02:46,290
So now we are going to replace zero this year with whatever the name value is and one with whatever
44
00:02:46,290 --> 00:02:47,370
the H value is.
45
00:02:47,790 --> 00:02:50,220
Let's run this and see if this is going to work as well.
46
00:02:50,220 --> 00:02:50,840
And we see.
47
00:02:51,060 --> 00:02:52,350
Hello, my name is Alfonso.
48
00:02:52,350 --> 00:02:53,630
I'm thirty one years old.
49
00:02:54,240 --> 00:02:56,580
So now for you is a little challenge.
50
00:02:57,150 --> 00:03:04,920
Create another variable up here, define one with a value assigned to it and use it inside of the string
51
00:03:04,920 --> 00:03:07,950
formatting real quick so you can use any value there.
52
00:03:10,240 --> 00:03:18,790
OK, so I'm going to create another string, I'm going to call this one job and it will be developer.
53
00:03:20,190 --> 00:03:31,350
Like, so I'm so-and-so years old, I'm a developer or not developer, but in curly brackets, too.
54
00:03:32,250 --> 00:03:37,230
So now it's going to be index to and I'm going to add the developer variable here.
55
00:03:37,230 --> 00:03:39,150
And actually it was called Java.
56
00:03:39,540 --> 00:03:42,560
So let me scroll out a little bit so we can see the entire line.
57
00:03:42,780 --> 00:03:45,470
So here it says, hello, my name is zero.
58
00:03:45,810 --> 00:03:48,810
I am one year old and I am two.
59
00:03:49,110 --> 00:03:53,910
So these numbers are, of course, not the actual values, but they're going to be replaced with whatever
60
00:03:53,910 --> 00:03:54,590
we have here.
61
00:03:54,870 --> 00:03:57,870
So I hope you try this and you were successful at trying it.
62
00:03:57,880 --> 00:04:02,100
So here I'm adding to another entry and that will say job.
63
00:04:02,290 --> 00:04:03,360
So let's see.
64
00:04:04,230 --> 00:04:05,310
Hello, my name is Alfonz.
65
00:04:05,310 --> 00:04:06,300
I'm 31 years old.
66
00:04:06,450 --> 00:04:07,560
I'm a developer.
67
00:04:08,220 --> 00:04:08,580
All right.
68
00:04:08,590 --> 00:04:09,970
So that worked great.
69
00:04:10,350 --> 00:04:14,350
Now let's look at string interpellation, OK?
70
00:04:14,470 --> 00:04:16,940
And I'm going to use it here.
71
00:04:17,700 --> 00:04:23,250
So this is the third concept of how you can manipulate your string, and that is called string interpolation.
72
00:04:23,790 --> 00:04:30,360
So string interpolation uses the dollar sign at the start, which will allow us to write our variables
73
00:04:30,360 --> 00:04:35,860
like this so directly use the variable name instead of having to use indexes.
74
00:04:36,390 --> 00:04:39,410
So here I'm adding the dollar at the beginning of the string.
75
00:04:39,630 --> 00:04:41,520
Hello, my name is then the name.
76
00:04:41,760 --> 00:04:44,730
I am eight years old now.
77
00:04:44,730 --> 00:04:50,910
You can leave name and age to appear at the end, but it's really not necessary at this point anymore.
78
00:04:50,910 --> 00:04:56,550
You can leave it out and you can run the code and you will see that it works the same way.
79
00:04:56,760 --> 00:04:59,900
So hello, my name is Alfonso and I'm thirty one years old.
80
00:05:00,540 --> 00:05:06,900
OK, so this is the more descriptive way why you don't have to use these indexes and you're never sure
81
00:05:06,900 --> 00:05:08,330
which variable was where.
82
00:05:08,520 --> 00:05:12,030
Well, that's not really the case, but still it's a little more complex.
83
00:05:12,210 --> 00:05:14,940
I believe that this is a rather good way of doing it.
84
00:05:15,480 --> 00:05:18,210
Well, you don't have to use Plus's all the time.
85
00:05:18,840 --> 00:05:22,800
And then we have number four, which are verbatim strings.
86
00:05:23,190 --> 00:05:27,060
OK, so let's look at those so verbatim strings.
87
00:05:27,750 --> 00:05:36,120
We need to use that and tell the compiler to take the string literally and ignore any spaces and escape
88
00:05:36,120 --> 00:05:38,610
characters like the back end.
89
00:05:39,270 --> 00:05:42,210
So let me show you what that means.
90
00:05:42,810 --> 00:05:47,280
So therefore, I need to have a slightly bigger statement here.
91
00:05:48,000 --> 00:05:49,890
Let me add that in there.
92
00:05:49,890 --> 00:05:56,550
Like so so now I have a super long string here and it has a lot of thought and maybe some text.
93
00:05:56,910 --> 00:06:03,240
So what this will do it it will on one hand ignore any spaces and escape characters like backslash and
94
00:06:03,240 --> 00:06:07,830
but at the same time, you see it takes the string literally.
95
00:06:07,830 --> 00:06:14,310
So wherever we have an empty space, let me add a couple in here, it's going to literally translate
96
00:06:14,310 --> 00:06:14,460
them.
97
00:06:14,460 --> 00:06:21,330
So here you see I have two more spaces or not spaces, but entire lines that were added at this point.
98
00:06:21,510 --> 00:06:26,390
So that's what you can achieve by using this add sign for word to use this backslash.
99
00:06:26,400 --> 00:06:32,250
And by the way, for example, here in the string formatting, what this will do is it will create a
100
00:06:32,250 --> 00:06:32,910
new line.
101
00:06:32,910 --> 00:06:35,160
So here it says.
102
00:06:36,640 --> 00:06:42,940
Hello, my name is so you see there is this line, break this backslash and creates a line break if
103
00:06:42,940 --> 00:06:46,540
you were wondering what this backslash and even means.
104
00:06:48,870 --> 00:06:52,360
And verbatim strings are very useful in specific cases.
105
00:06:52,380 --> 00:06:53,290
So let me show you.
106
00:06:54,360 --> 00:07:00,600
So here, instead of using backslash backslash to write file parts, we can use verbatim strings to
107
00:07:00,600 --> 00:07:01,390
make it easier.
108
00:07:01,440 --> 00:07:09,090
So if you remove the ad, you will get an error because you backslash a backslash are not valid escape
109
00:07:09,090 --> 00:07:10,180
characters, for example.
110
00:07:10,800 --> 00:07:12,810
So here you need to have this.
111
00:07:12,810 --> 00:07:17,460
And because otherwise it will think, OK, we're trying to escape something, which means we're trying
112
00:07:17,460 --> 00:07:23,570
to use this backslash you, as we've seen with the backslash end, for example.
113
00:07:23,580 --> 00:07:23,820
Right.
114
00:07:23,840 --> 00:07:32,490
So here we used backslash and well, I got rid of it, but we had this backslash and it would escape
115
00:07:32,490 --> 00:07:38,640
the character and meaning it doesn't consider this backslash and as a character, but it just jumps
116
00:07:38,640 --> 00:07:40,440
to the next line or whatever.
117
00:07:40,710 --> 00:07:47,900
So what this would do in here is it will now give us this particular path and it will give us the dog
118
00:07:47,910 --> 00:07:48,390
photo.
119
00:07:49,000 --> 00:07:50,970
OK, so that's where it is.
120
00:07:51,240 --> 00:07:52,460
Sign is super useful.
121
00:07:52,710 --> 00:07:59,250
So whenever it comes to paths on your machine and then with verbatim strings, even valid escape characters
122
00:07:59,250 --> 00:07:59,760
won't work.
123
00:07:59,970 --> 00:08:06,970
So here, given this backslash and will not do anything, it doesn't have any powers in a verbatim string.
124
00:08:07,200 --> 00:08:15,210
So if we're using that sign here, you can see Moha backslash and you have no powers here, even though
125
00:08:15,690 --> 00:08:17,040
it does have power.
126
00:08:17,100 --> 00:08:21,150
So let me show you in another example.
127
00:08:22,860 --> 00:08:26,160
Without the ads and now we have this backslash and.
128
00:08:27,650 --> 00:08:30,990
Then we will see Wahaha backslash.
129
00:08:31,550 --> 00:08:37,059
You have no powers here, even though I should say you have powers here, for example.
130
00:08:37,070 --> 00:08:38,090
So let's run it again.
131
00:08:38,090 --> 00:08:40,909
And you see Wahaha backslash and you have no powers.
132
00:08:40,940 --> 00:08:46,310
That's where we're using the outside and and Wahaha backslash end, meaning Lambrick.
133
00:08:46,310 --> 00:08:47,400
You have powers here.
134
00:08:48,080 --> 00:08:48,470
All right.
135
00:08:48,710 --> 00:08:53,630
So these are the different ways of string manipulation.
136
00:08:54,050 --> 00:09:02,120
And while for the first three, I would say it makes sense to decide for one of those and to use this
137
00:09:02,120 --> 00:09:08,330
consistently when it comes to verbatim strings, they really have special use cases, as you have seen,
138
00:09:08,330 --> 00:09:12,390
for example, with the location on your PC.
139
00:09:12,410 --> 00:09:14,200
So with the firepower, for example.
140
00:09:15,050 --> 00:09:15,430
All right.
141
00:09:15,440 --> 00:09:16,580
So that's it for this video.
142
00:09:16,790 --> 00:09:17,810
See you in the next one.
13313
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.