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