Welcome back to Section four.
Now we're going to cover lesson two string manipulation.
So in the last lesson, we learned just what strings are there.
Basically these lines of text in quotes, single or double quotes.
And now we're going to learn some ways we can mess with these strings, mess with this text.
And there are a number of ways to do that.
So one is concatenating strings and there are a number of ways to concatenate actually, you can use
the concat method.
You can use the plus sign, there's also a way to do it with string interpolation, but basically you
can do multiline strings and that's totally fine.
And you can do template literals and expression interpellation, so we're going to look at all of that
over here in JavaScript.
Here's the example right here.
So string concatenation, let's say if we started with a variable, my name is that's one string we
can can Katani with the plus sign.
And do an empty string there, so just a space concatenate again.
Our final word, phyto.
So if we do that separately, we can do.
My name is and then string plus equals.
So that's using the assignment operator.
The addition assignment operator to do equals string plus that space and then string equals string plus
phyto.
These accomplish the same thing.
You can also if you have two separate strings, you can concatenate multiple values using the concat
method and the method is basically a function.
So a block of code that you can run on a specific object.
And this is a this is a specific object right here.
We're going to contaminate these values and add those to string one, OK?
So that's going to accomplish the same thing there.
And then you can also do multiline strings, so this is where your console logging multiple lines using
the new line character to indicate a new line.
This backslash is useful to escape various characters for special operations like this, where you want
to have a new line and then here you can do the same thing if you use these special back ticks right
here.
It's like this special quote and you're going to find this at the top left corner of your keyboard,
just below your escape key.
You can use this for string interpolation.
You can also use it for multiline strings without having to use the new line character and then template
literals.
What this means is that you can do interpellation.
Basically, these back ticks indicate a template and you can interpellation means you are going to reference
some variable or other nickname property like a like a parameter or an object property directly inside
the string.
And you're going to use the special syntax dollar sign curly braces and then the name of the shortcut
value like the variable, and then you end it with a curly braces right there.
And that's the syntax for interpolating.
So it's to put something inside of the string.
OK, now let's take a look at all that in JavaScript.
So let's start again with String Concat Nation.
So let's just look at string concatenation now.
Concatenation, really what this refers to is just.
Piecing multiple strings together and there are a few different ways we can do this, so let's just
start with one
and we can say my dog's name
plus is
Bones.
That's the name of my dog.
All right.
My dog's name is Bones.
Now, to test this, we could go ahead and log and make sure that we're getting what we expect.
Let's just call a long string one and save.
And then run it.
My dog's name is Bones, it concatenated the string together using the addition symbol right there.
Let's try to do the same exact thing.
Well, let's do it in a little bit different way.
The string to.
So let's start with again my dog's name.
And now we're going to do something a little bit different.
We're going to modify screen to we're going to manipulate it this way.
So after we've previously assigned it, we're just going to do it plus equals.
And now in this one, we're going to see is and we can do that again.
But this time we're going to do the name of the dog at the end.
So you can see the structure is very similar.
OK, and this plus equals is the same as saying string two equals string two plus and then my string.
OK, you can see those are the same.
That's why we use this addition assignment operator to contract that process because we don't want to
have to do the repetition.
Don't repeat yourself.
That's one of the principles in programming.
Don't repeat yourself D-R.
Why we call that the dry method.
OK, so if we do that, we try to console along that.
In fact, we can comment that one out, actually, let's keep that in, just so you can see there are
two at the same time.
So that and run it.
So you get the same thing twice, this one right here and this one right here, the both accomplishing
the same thing, gluing these together and now we're going to let string three equal.
My dog's name.
OK, and now we're going to do.
Let string four, equal string three, dog and cat.
It doesn't have to be string four, we could do string three again, but we do dot com cat is
three and now is that.
Doing string for this time, because that's the final product.
My dog's name is Bones, OK, again, we could have just done string three would have been the same.
You can reassign more values to.
String theory, OK, string theory is already been declared, so we don't need to let twice.
There you go.
OK, so that's string concatenation, that's just gluing strings together.
Now let's take a look at multiline strings
strings.
What that means is the string is going to go down onto multiple lines.
So let's just consider
this line.
And now we're doing the special new line character, as you can see, it colored it differently inside
my editor, and now we actually don't need to do anything else.
We don't need to put this on multiple lines in our code.
But it's useful just to know what we're what we're trying to do.
So we can say second line, too, and that's it if we do that.
And run it, it puts it on two separate lines, so kind of as a nice note for ourselves, we could just
remember that that's what we're trying to do.
And we put those on two separate lines.
How many spaces before doesn't matter, these spaces don't mean anything, so that'll work just fine.
And we can do a similar thing with council log, but this time we're going to use our special tactics.
So in this case, the formating does matter.
So we're going to we're going to see first line here and we're going to do a new line.
And then the second one, OK.
Here you go, one, two.
OK, so there you go.
So because we're using these special back ticks, it's going to remember the formatting that we've put
in this place.
So it's going to understand or pass the string in a slightly different way.
All right.
But that's how the two ways you can do multiline strings.
So now let's learn a little bit more about template literals.
And interpellation
interpellation is another way you can use you can put multiple pieces of data together in strings.
OK, for example, we're going to do let string four equal phones, OK?
So I'm going to keep that handy.
It's just going to be sitting right there and now we'll do a let's string five equal use these tactics.
And we're going to do my dog's name is, and then we use our special dollar sign, Curly Braces String
for.
And then we end it with the back tick.
Now, if we can't so long that
story five
and see what happens, what we should get is the same line again, this time using interpolation.
So what happened here is it starts reading.
It says sees this and says, oh, I'm going to do some special formatting in here.
If I'm multiline, then I'll multiline.
But if I see anything with this syntax, a dollar sign and a curly braces, I'm going to try to reference
that information from somewhere previous in my code.
So there you go.
It sees that there's a string for and there's an assigned value to that.
So it takes that assigned value and puts it right in the spot.
OK, that's what happens when you consider the string it goes and sees, oh, what's this string defined
as it goes and finds out, finds out that it requires another reference and then it goes and grabs that
he puts those together and then you're done.
So that is string manipulation.
We used one method right here, but there are many string methods that you can look at.
They're all here in this one resource.
If you go to the Murdochs, you can go and see string talks all about creating strings and various different
escape notation that you can use inside strings.
And then multa multiple lion strings and then these different methods you can use, you can find a certain
character at a specific index you can concatenate.
You can find out if a string includes a certain search string.
Find out if it ends with certain values, et cetera.
Lots of different ways.
You can modify information in strings, find information inside of strings.
And so I recommend you try a lot of these different string methods in your practice.
OK, we'll see in the next video.