Jonas: Let's continue with some simple string methods,
and 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.
And here you see that indeed they transform the string
all to lowercase and to uppercase.
And 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.
... in name, okay.
So let's say that the passenger,
when he checked in, misspelled his name,
and so it looks like this now.
And 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,
and here we only need the first letter.
So how do we do that?
We take the first letter by using the index zero.
And so this one, we now convert to uppercase, all right.
And 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.
And so here we then put back together
just the first part of the string,
converted to uppercase here,
and then the rest of the string
that we already had to lowercase before.
Okay, and indeed now it is corrected. Great.
And of course, we could have done a function with this,
and so maybe if you have the time you can do that.
So just create a function that 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 the 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.
And 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 different letters here,
then, of course, they would not be the same,
but here it's just a matter of capitalization.
And for emails, I believe that they are still valid
even with the capitalization off.
And 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 whitespace here.
So these empty spaces and also this enter, basically, here
also count as whitespace.
So there is even a method for that.
So, let's say, trimmedEmail, and so that is lowerEmail.trim.
All right, let's just now log this
through the console, just to see.
And 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.
And so, again, I will start from loginEmail
and then do it all in one step.
.toLowerCase.
And now this is a bit similar to what we did before
with the map set method.
So this here will return a new string,
and on strings we can call string methods.
And therefore, right here,
we can now immediately call the trim method.
So normalizedEmail, and that should look exactly the same.
And it does, so let's get rid of this.
And these two I will simply leave as a comment here.
And now, just to finish, let's actually also compare them
for the sake of completion.
And once again, you could do this here
in a separate function.
So you could do a function for this behavior
where you pass in two 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.
And by the way, here, since ES2019,
there's also trimStart and trimEnd,
which, as their names say, you can use to trim whitespace
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.
And 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£.
And 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.
And so that's actually what we're gonna do now,
we want to compute the price in the US.
All right, and so for that we will need to replace
the pound with the dollar sign
and the comma with the period.
So that's easy enough, priceGB.replace,
and now here two arguments.
The first one is the one we want to replace,
and let's start here with the pound sign.
And the second argument is the string
that will replace the first one.
So let's take a look, priceUS.
And so that already worked.
And now we need to replace also the comma 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.
And so, on that string
we will immediately call the next replace method.
And now, we are replacing the comma with the dot.
And so, indeed, now we have the price in US.
But, indeed, we can also replace entire words,
not just single characters.
So let's create an announcement
that you would typically hear at the airport, like,
"All passengers come to boarding door 23. Boarding door 23."
However, usually, it is called a gate and not door,
and so, let's replace that
and log the results 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.
And so here, it's the same,
and so we need to log the actual result to the console.
So, replace door with gate.
Here it's boarding, and now let's check it out here.
So here, boarding gate,
but then here you actually still have boarding door.
And so this replace only replaced
the first occurrence of this search string here.
And actually, that's completely normal,
that's just how the replace method works.
So, again, this method will only replace
the very first occurrence of this search string.
So in this case the door,
but the rest of them will simply stay the same,
so they will not get updated.
But fortunately for us, there's also the replaceAll method,
which, as the name says,
will actually replace all the occurrences
of the search string.
So in this case, it will replace all the doors
with gates, basically.
Now, at the time that I was recording this video
this method wasn't available in JavaScript yet,
and so that's why here we get this error.
But never mind about this,
because now that you're watching this video
this will actually work,
and so you should definitely go ahead
and use this solution right here,
so, the replaceAll method,
whenever you need to replace multiple occurrences
of a certain substring inside the bigger string.
Now, besides this solution, there's also another solution
that was available before we had the replaceAll method,
which involves using a regular expression,
which is what I'm going to show you next.
Now, regular expressions are one of the most complex
and confusing topics of programming,
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 let'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,
and so we replaced gate here and here.
All right, and 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, and now as a final topic of this lecture,
there are three simple methods that return booleans.
So, booleans, okay.
And these methods are includes startsWith and endsWith,
so let's create a new plane here,
and that's gonna be the A320neo.
And I'm just missing here the assignment.
And 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, and let's test
if it includes the string A320.
And indeed, it does.
So this airplane here is the Airbus A320neo,
but it still includes just the A320.
Now, let's say if it includes Boeing,
and of course it doesn't.
And now there's another one, which is the startsWith.
And this one is also actually very much used,
I remember using it very recently.
And so in this case here it is false, all right.
But then if we change this here to Airbus,
then it is indeed true.
So 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.
And 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.
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.
And 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.
And we also need to check if it ends with neo,
because that means that it's part
of the new Airbus family.
And so we can also use endsWith.
So, as always, these methods here
that return booleans are especially helpful
for conditionals like this,
Part of the NEW Airbus family.
And indeed, this one here is.
But, of course, if it was just a regular A320,
then that would not get logged 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.
And let's now actually practice that a little bit
with some kinds of-
yeah, we can say, a practice exercise.
And so here we want to check if the baggage
of a certain 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,
and 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 note,
"Got some snacks and a gun for protection."
Okay, so maybe some passenger would bring a gun to a plane,
but so now let's make sure
they will not get aboard the plane.
And, actually, let's change some things here.
So let's make this uppercase,
let's say this person likes to write
uppercase for some reason.
And 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 lowercase,
because that makes it a lot easier
to then compare it to something.
(indistinct) say baggage here.
And so, baggage is basically items.toLowerCase.
So this one we use all the time.
And now we can just check if the baggage
includes a knife or a gun,
and in that case the person is not allowed on board.
So, if baggage.includes,
and so here is that method again, which returns a boolean.
So, if it includes knife or if it-
so baggage includes a gun, then we say,
"You are not allowed on board."
else, let's log "Welcome aboard."
And 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 uppercase.
And 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.
And 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 delete this part for now.
So if we didn't have the toLowerCase,
then the first person would also be "Welcome aboard".
And again, that's because knife here, uppercase,
is different from this knife.
And 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.
And so that would be highly impractical,
and so we just turn everything to lowercase.
So, 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,
and I'll see you back here in the next video.