‫Welcome back.
‫And this video.
‫I would like to teach you about string manipulation.
‫And that's going to be quite important because you are going to need it throughout your coding experience
‫and career.
‫So this is an extra lecture where we are going to talk about the different ways of printing data on
‫the screen, and we're going to look at composite strings, string formatting, string interpolation
‫and verbatim strings.
‫So of course you can stick to one way if you prefer one, but if you like all of them, then it's fine.
‫But try to stick to one style throughout your project.
‫So let's first of all define a few variables.
‫So inside of my main method, I'm going to create two new variables.
‫One is going to be H, and then the name will be Alfonso.
‫So we have an integer in H and the string name.
‫Now let's first of all use string concatenation and I have covered this keyword before.
‫Let's look at this real quick.
‫So here we are using string concatenation where we say, hello, my name is and then the plus sign,
‫the variable plus and then another string, so to speak, plus another variable plus another string,
‫so to speak.
‫So we have used this in our double integer example where we basically said what kind of variables we
‫are adding together in order to get a sum.
‫So this is string concatenation, so let's run this and look at it and we will see it says string concatenation.
‫Hello, my name is Alfonso.
‫I am 31 years old, so this works flawlessly with integers as well as with strings here.
‫So this is a string and this is an integer and we are using it in string concatenation and there is
‫no problem with it in our console.
‫Right line statement here.
‫All right.
‫Now let's look at the string formatting concept.
‫This is going to be a little more advanced, so let's look at it.
‫So string formatting uses the index.
‫So here I have a statement that says that this is string formatting.
‫Then we also use Hello, my name is but this time we don't end the string and use concatenation, but
‫instead we're using curly brackets, then index zero for the first entry and index one for the second
‫entry.
‫So what we are saying is whatever is going to come after this comma here after my string at position
‫zero.
‫So the first entry will be positioned here.
‫And whatever is positioned at the index one which is the second entry will be positioned here.
‫So now we are going to replace zero this here with whatever the name value is and one with whatever
‫the value is.
‫Let's run this and see if this is going to work as well.
‫And we see.
‫Hello, my name is Alfonso.
‫I'm 31 years old.
‫So now for you is a little challenge.
‫Create another variable up here, define one with a value assigned to it and use it inside of the string
‫formatting real quick so you can use any value there.
‫Okay.
‫So I'm going to create another string.
‫I'm going to call this one job and it will be developer.
‫Like, So I'm so-and-so years old.
‫I'm a developer or not a developer, but in curly brackets too.
‫So now it's going to be indexed too.
‫And I'm going to add the developer variable here.
‫And actually it was called Java.
‫So let me scroll out a little bit so we can see the entire line.
‫So here it says, hello, my name is is zero.
‫I am one years old and I am two.
‫So these numbers are of course not the actual values, but they're going to be replaced with whatever
‫we have here.
‫So I hope you try this and you were successful at trying it.
‫So here I'm adding another entry and it will say job.
‫So let's see.
‫Hello, my name is Alfonso.
‫I'm 31 years old.
‫I'm a developer.
‫All right, so that worked great.
‫Now let's look at string interpolation and I'm going to use it here.
‫So this is the third concept of how you can manipulate your string, and that is called string interpolation.
‫So string interpolation uses the dollar sign at the start, which will allow us to write our variables
‫like this so directly used variable name instead of having to use indexes.
‫So here I'm adding the Doppler at the beginning of the string.
‫Hello, my name is then the name.
‫I am a years old now you can leave name and aged up here at the end, but it's really not necessary
‫at this point anymore.
‫You can leave it out and you can run the code and you will see that it works the same way.
‫So hello, my name is Alfonso and I'm 31 years old.
‫So this is the more descriptive way where you don't have to use these indexes and you're never sure
‫which variable was where.
‫Well, that's not really the case, but still, it's a little more complex.
‫I believe that this is a rather good way of doing it, where you don't have to use pluses all the time.
‫And then we have a number four, which are verbatim strings.
‫So let's look at those.
‫So verbatim strings, we need to use the at and tell the compiler to take the string literally and ignore
‫any spaces and escape characters like the backslash.
‫N So let me show you what that means.
‫So therefore I need to have a slightly bigger statement here.
‫Let me add that in there like so, so now I have a super long string here and it has a lot of lower
‫MIP some text.
‫So what this will do is it will on one hand ignore any spaces and escape characters like backslash n,
‫but at the same time, you see it takes the string literally.
‫So wherever we have an empty space, let me add a couple in here, it's going to literally translate
‫them.
‫So here you see, I have two more spaces or not spaces, but entire lines that were added at this point.
‫So that's what you can achieve by using this sign.
‫If I were to use this backslash n by the way, for example, here in the string formatting, what this
‫will do is it will create a new line.
‫So here it says.
‫Hello.
‫My name is so here.
‫You see there is this line break this backslash n creates a line break if you were wondering what this
‫backslash n even means.
‫And verbatim strings are very useful in specific cases.
‫So let me show you.
‫So here, instead of using backslash backslash to write file paths, we can use verbatim strings to
‫make it easier.
‫So if you remove the edge, you will get an error because backslash you backslash a backslash D are
‫not valid escape characters, for example.
‫So here you need to have this edit because otherwise it will think we're trying to escape something
‫which means we're trying to use this backslash u as we've seen with the backslash n for example.
‫Right?
‫So here we used backslash.
‫N Well I got rid of it, but here this backslash.
‫N It would escape the character.
‫N Meaning it doesn't consider this backslash.
‫N as a character, but it just jumps to the next line or whatever.
‫So what this would do in here is it will now give us this particular path and it will give us the dog
‫photo.
‫So that's where this ad sign is super useful.
‫So whenever it comes to paths on your machine and then with verbatim strings, even valid escape characters
‫won't work.
‫So here, even this backslash n will not do anything.
‫It doesn't have any powers in a verbatim string.
‫So if we're using the ad sign here you can see more haha backslash and you have no powers here even
‫though it does have power.
‫So let me show you in another example.
‫Without the ad.
‫And now we have this backslash n.
‫Then we will see more.
‫Ha ha.
‫Backslash.
‫You have no power here, even though it should say you have powers here, for example.
‫So let's run it again.
‫And you see what?
‫Ha ha.
‫Backslash n you have no powers.
‫That's why we're using the add sign.
‫And here.
‫Ha backslash n meaning line break.
‫You have powers here.
‫All right, so these are the different ways of string manipulation.
‫And while for the first three, I would say it makes sense to decide for one of those and to use this
‫consistently when it comes to verbatim strings, they really have special use cases, as you have seen,
‫for example, with the location on your PC.
‫So with the file path, for example.
‫All right.
‫So that's it for this video.
‫See you in the next one.