Let's continue with some simple string methods.
The first two are gonna be for changing the case
of a string.
So that's airline.toLowerCase,
and this doesn't require any arguments at all.
And then we also have toUpperCase.
And these, we use all the time
as you will see soon in a more real world example.
Here you see that indeed they transform the string
all to lowercase and to uppercase.
Again, you can, of course, call this directly,
also on a string, so like this.
All right.
But now, let's use this on a more practical example
to fix the capitalization in a passenger name.
Zation in name.
Okay.
So let's say that the passenger, when he checked-in,
he misspelled his name, and so it looks like this now.
So this needs to be fixed.
It should look like this.
Okay.
So in order to fix it,
the first step is usually to put everything
into lowercase here.
So const passengerLower is passenger.toLowerCase like this.
Okay.
So basically, we already have now this part here.
And now, all we need to do is to essentially take
the first letter and then the rest of the string
and put these two parts back together.
So passengerCorrect will be passengerLowerCase.
Here, we only need the first letter.
So how do we do that?
We take the first letter by using the index zero.
So this one, we now convert to uppercase.
All right.
Now, we simply need to add the rest of the string.
So again, we take passengerLower.
And then we use the slice that we learned
in the last lecture and start at position number one,
because remember, that will then go to index one
and take everything until the end of the string.
So here, we then put back together just the first part
of the string converted to upper case here,
and then the rest of the string
that we already had converted to lowercase before.
Okay.
Indeed, now it is corrected.
Great.
Of course, we could have done a function with this.
So maybe if you have the time, you can do that.
So just create a function which takes any passenger name
and then returns the correct one.
So with the fixed capitalization.
But anyway, I will now do another real life example,
which is to check a user input email.
Okay.
So check email.
Now, this will not be really a checking.
It will more be a comparing.
So comparing emails.
So let's say that we have to email of one passenger.
So hello@jonas.io.
But now as the user logs in,
so let's say loginEmail.
Accidentally, they do it all wrong
and they start with a couple of spaces here.
And then the capitalization is all completely off like this.
And then maybe even another space
or even an enter character, which remember is \n.
So I think we talked about that in the beginning.
Okay.
But still this email here is actually still kind of correct
and kind of valid.
So we can now still compare
if these two are kind of the same.
Okay.
So we can now try to convert this one to this one
and then check if they are the same,
because essentially, again, they are.
They would be different
if we had like different letters here,
then, of course, they would not be the same,
but here it's just a matter of capitalization.
For emails, I believe that they are still valid
even with the capitalization off.
So when we check user input like this,
the first step is usually to convert it to lowercase.
So lowerEmail is loginEmail.toLowerCase.
And then we must also get rid of all the wide space here.
So these empty spaces.
Also, this enter basically here also counts as a wide space.
So there is even a method for that.
So let's say trimmedEmail.
So that is lowerEmail.trim.
All right.
Let's just now lock this through the console just to see.
So that looks exactly like that first version.
Now, just look closely at what we did here.
So we called toLowerCase on this loginEmail,
stored it to this variable.
And then on that variable, we called the trim method.
But why do we even need this intermediate email?
We could do this all in one step.
So basically, we can replace all of this
and let's call this normalizedEmail actually.
So again, I will start from loginEmail,
and then do it all in one step.
.toLowerCase.
Now, this is a bit similar to what we did before
with the map set method.
So this here will return a new string, right?
On strings, we can call string methods.
Therefore, right here,
we can now immediately call the trim method.
Okay.
So normalizedEmail,
and that should look exactly the same.
And it does.
So let's get rid of this and this.
I will simply leave as a comment here.
Now, just to finish, let's actually also compare them
for the sake of completion.
Once again, you could do this here in a separate function.
So you could do a function for this behavior
where you pass in to emails, one, which is the correct one,
and one, which is the one to check.
And then you could return either true or false from that.
So again, that sounds like a good challenge
that you can take if you want.
By the way, here, since ES2019, there's also trim start
and trim end, which as their names say,
you can use to trim wide space only from the start
of the string or only from the end.
Next up, let's learn one of the most important thing
about strings, which is to replace parts of strings.
So replacing.
Okay.
We're gonna stay here again with the airlines.
So let's say we have the price of a flight,
and that price is for Great Britain.
So let's say 288,97 pounds.
You can use any other currency sign that you want here.
What's important is that here in Europe,
we use the comma as a decimal separator,
unlike in the US where you use the period.
So the dot.
So that's actually what we're gonna do now.
We want to compute the price in US.
All right.
So for that, we will need to replace the pound
with the dollar sign and the coma with the period.
So that's easy enough.
PriceGreatBritain.replace.
Now, here two arguments.
The first one is the one we want to replace.
And let's start here with the pound sign.
The second argument is the string
that will replace the first one.
So let's take a look.
PriceUS.
So that already worked.
Now, we need to replace also the coma with the period.
So we could create another variable now,
but instead we will do chaining again
just like we did before, and for the same reason,
because this returns a string.
So on that string,
we will immediately call the next replace method.
Now, we are replacing the comma with the dot.
So indeed now, we have the price in US.
Okay.
But indeed, we can also replace entire words,
not just single characters.
So let's create an announcement
that you would typically hear on the airport.
Like all passengers come to boarding door 23.
Boarding door 23!
However, usually it is called gate and not door.
So let's replace that
and lock the result to the console immediately,
because just like the other method that we studied,
replace also creates a brand new string.
It doesn't mutate the original one.
So here is the same.
So we need to log the actual result to the console.
So replace door with gate.
Here, it's boarding.
Now, let's check it out here.
So here, boarding gate,
but then here, you actually still have boarding door.
So this only replace the first occurrence
of this search string here.
Okay.
Actually, that's completely normal.
That's just how it works, but we can still go around this.
Now, very soon, JavaScript will have a method
called replace all, which will work like this,
but right now here, it should not work yet.
Indeed, I get here replace all is not a function.
But if you're watching this video in the future,
then this year might already work for you.
So please go ahead and try it out.
Alright.
But for me, it doesn't work yet,
but there is still another solution,
which is to use something called a regular expression.
Now, regular expressions are one of the most complex
and confusing topics of programming,
but we will still take a short look at him
at some point in the course.
But for now, we are just gonna use
a very simple regular expression to tell the replace method
that it should actually target all the occurrences
of door here and not just the first one.
So that's, again, copy this here.
And then to create a regular expression,
we need to write the string between slashes.
So not between quotes.
So it turned yellow here again,
which means that it's some kind of string,
but it is now a so-called regular expression.
Then in this case, all we want to do
with this regular expression is to add this G flag here,
and this stands for global.
And that's actually it.
So right now, all the occurrences will be targeted.
So we replaced gate here and here.
All right.
Just as a side note here, once more,
the replace method is, of course, also case sensitive.
So just like all of the other string methods are.
Okay.
Now, as a final topic of this lecture,
there are three simple methods that return booleans.
So Booleans.
Okay.
These methods are includes starts with and ends with.
So let's create a new plane here,
and that's gonna be the A320neo.
I'm just missing here the assignment.
Now, we can simply use includes
just like we can in arrays one more time.
So they have a very similar structure here
in terms of methods that we can use on them.
So includes.
Let's test if it includes the string A320,
Indeed, it does.
So this airplane here is the Airbus 320 Neo,
but it still includes just the A320.
Now, let's say if it includes Boeing.
And of course, it doesn't.
Now, there's another one, which is the starts with.
This one is also actually very much used.
I remember using it very recently.
So in this case here, it is false.
All right.
But then if we change this here to Airbus,
then it is indeed true.
Okay.
These three letters here
are indeed the first three also here.
So it doesn't have to match the entire word.
So just Ai would also be true,
but Aib, let's say, would then be false,
because, of course, the string does not start
with these three letters.
With Airb, then it would again be true.
Okay.
Of course, this A320 is also still true
as long as it is somewhere in the string.
So now, it's somehow here in the middle.
Okay.
So let's try something very simple here.
So let's say that we want to check
if the current plane is part of the new airbus family.
So all we need to do to check that is to see
if the plane name starts with airbus.
And we can already confirm that it is, right?
We also need to check if it ends with neo,
because that means that it's part of the new airbus family.
So we can also use ends with.
So as always, these methods here that return booleans
are especially helpful for conditional like this.
Part of the new airbus family.
Indeed, this one here is.
But of course, if it was just the regular A320,
then that would not get locked to the console.
So whenever you need to take some decision
based on the contents of the string,
these three methods are very, very helpful.
Let's now actually practice that a little bit
with some kinds of...
Yeah, we can say a practice exercise.
So here, we want to check if the baggage officer
and passenger is basically allowed to be checked-in.
So to be allowed on the plane.
So checkBaggage, function.
And then here, we get the items.
Let me, again, first call the function
to show you what I mean.
So basically, the passenger here says what they have.
So I have a laptop, some food and a pocket knife.
Then let's call this again
and say, I have some socks and a camera.
And then let's call it a third time with the string now,
got some snacks and a gun for protection.
Okay.
So maybe some passenger would bring a gun to a plane.
So now, let's make sure they will not get abort the plane.
And actually, let's change some things here.
So let's make this upper case.
Let's say this person likes to write upper case
for some reason.
Okay.
So this is important to show you once again
that when we receive input from a user,
we usually always start by putting everything
into lower case, okay?
Because that makes it a lot easier
to then compare it to something.
Well, let's simply say baggage here.
So baggage is basically items to lower case.
So this one we use all the time.
Now, we can just check if the baggage includes a knife
or a gun.
In that case, the person is not allowed on board.
So if baggage.includes.
So here is that method again, which returns a booleans.
So if it includes knife, so baggage includes a gun,
then we say, you are not allowed on board.
Else.
Let's log welcome aboard.
All right.
So now, let's see here why it is so important
to convert everything first to lowercase.
So in this example here, this first person has knife,
but with an upper case.
So if we didn't convert this to lowercase,
then this includes here would be false,
because the lowercase knife is different
than the uppercase knife.
Okay.
So let's see the result.
So indeed, this person has a knife,
so they're not allowed.
This person here is fine.
And then this one has a gun
and so of course, they're also not allowed.
But now, if we took away this here,
well, then our code is not really gonna work.
So let's just to leave this part for now.
So if we didn't have the two lowercase,
then the first person would also be welcome aboard.
Again, that's because knife here uppercase is different
from this knife.
So then we would have to check for all the variations,
like knife like this, or this knife, or even this.
So every possible variation.
So that would be highly impractical.
So we just turn everything to lowercase.
Again, so that we can then easily compare it
with one standard, which is this and this.
All right.
Perfect.
So that was another fun lecture, if you ask me,
and I hope you are feeling the same.
Now, it's time for a break.
I see you back here in the next video.