Hey there, this lesson is going to be all about string methods in JavaScript, looking at trim, charging
the case to upper, lower case, splitting, slicing, replacing, lost index of index of.
There's a lot of options when it comes to strings covering those within the section.
So string methods, they allow you to update and manipulate string values.
You can search, you can slice.
So there's a lot of things that you can do that is similar to what you can do with the race where we
do have a paragraph here.
So we've got a string value here.
We can search for a particular term and then look and find the index value of that term.
You can also see that we can get that back so we know which the index value is and where it's occurring
within the string.
So it's finding the first index value of its returning back, just that one.
There's also some additional options with strings.
And as you can see, there's quite a bit that you can do with string methods.
There's a bunch of examples here where it's looking for index value of blue.
So it's returning zero if it's not finding it.
So because blue is the first value within the string, if we're looking for blue, it's going to return
back a negative one.
So that means that it wasn't found within the string, if we're looking for well, comma zero.
So it's going to return back five, which is going to be the first character of Blue Whale is if we're
looking at well, five, we're also going to be returned back five.
So this is a parameter that you can pass in because the whale is starting at five.
But if we start at seven, then we're going to get a negative one because it's not going to be able
to find that within the.
And here's another example of checking to see if Blue is contained and noticed that it is case sensitive.
So blue lowercase is not the same as blue uppercase.
So it's going to be returning back a negative one.
And we're also can check to see if it doesn't exist.
So if we're not getting a negative one because it does exist.
So it's true.
And as you can see, there's a number of ways to check so you can have negate it as well as equal to
negative one and a number of different ways to check to see if something exists within a string.
Some more examples.
So in this example here, it displays four because E is within the fourth position index value of that
particular string value.
Let's try this out within our ED.
So now let's create a string in the ED.
And this can contain any content that you want.
You can search for the same values.
And I'm going to be looking for create your own string.
And I have some numbers in there and we'll repeat JavaScript twice so I can show you that there are
different ways if you want to return back several different words within that string.
So refresh and we've got my string value output.
It it is definitely a string value in there.
We can get the length of the string.
So doing length we can see how many characters are contained within the string.
A lot of these all out into the console.
So my string getting the length also trim the string.
So if for instance we've got some spaces in the front and in the back, we can use the trim method and
what that will do is that's going to remove out all of that white space there.
So see that now we've got the trimmed version of it using the my string trim, whereas if we just put
out the string, you can see that it is slightly different.
So we've got all of the white space and that one, there's no white space in this one.
So that's what trim can do it.
It allows you to trim your string.
You can also convert it.
So converting the case so there's two uppercase to lowercase, so using to lowercase that will turn
all of the string values to lowercase.
As you can see in the example, you can also use several of them together if you want to.
So trim and trimming them, as well as converting them to lower case, you can chain them together in
that format.
There's also, as we saw, lowercase.
And then there's obviously there's uppercase as well.
So we can convert it to uppercase and they'll do the same thing, except it will do the opposite.
Return all of the string values to uppercase.
We can also split string values.
So we've got our string that we've been using and if we want to split it into an array, so splitting
it by the spacing.
So this will split out all of the words into an array.
And this is a useful function because then you can use this in order to search out for different words
or you can loop through them.
And then you could apply a bunch of the array methods that are also really super useful.
So turning a string into an array, this is a great one.
How you're splitting up so you can split it by commas or you can split it by different letters.
So whatever works.
So in this case, we split it by the comma.
If I want it to, I could take a character.
I could split it by that as well.
And you can see that it's got all split via the eight.
And notice another thing to when it does the split that it actually removes the character from that
array.
So in this case, if we're removing the spaces, we don't.
See any of the spaces, we just see the words, so if you want to find out which character is at index
value nine, for example, you can see that it's going to return back R because R is in the ninth spot.
That's the one that's returning are going to comment.
These treat a string just as you would an array.
So if you want to get whatever character is at nine, so just as we do character add, we can do that.
My string nine, just getting it by the index value can also use my string to return a part.
So we know that R is in the ninth spot and if we take the seven spot, so that should give us the W
if we want to just return back world, we can extract that from the string using slice where we can
have a starting position.
And in this case we did want to start it at seven.
So refresh and we get the string back starting at the seventh position.
You can also specify what index value you want to stop the slide.
So let's try 12 and we refresh.
So that gives us the ability to pull out the word world out of the string.
We also have another way to do this or do some similar functionality where we can select substring and
substring returns, a subset of the string between one index and another or through the end of the string.
So works the same way as we saw with Slice, where we're taking a substring value, where if we want
to have a starting position of seven and an ending position of twelve, you can see it returns back
world as well.
There's also one more option.
So there's quite a few options here.
And this is a shorter format of substring, which returns back characters in the string, beginning
at a specified location through the number of characters.
So if we do substring 12, you're going to see that returns back 12 characters.
So if you want to only return back world, you can use substring seven, starting position seven and
returning back five characters after that.
And that returns McWorld.
So there are slight differences to these and depending on what the situation is, you can use either
one in order to accomplish what you're trying to find.
You can also replace content within the string.
So if we want it to, for instance, outputs and I'm going to create another one where we're just going
to output these values into the console, because I know we've got quite a lot going on in this console.
So setting up and I'm going to have a variable called output and we're just going to declare that for
now.
And then within the code, we're going to set a value of output and then logout output into the console
and we'll just output that.
So that's our current string that we're using right now.
And if we want it to replace content within the string, we can use the replace method.
So it looks for a pattern within a string and then does the replacing.
So this is another really useful one.
So if you want to look for the word love and change it to enjoy, so maybe you find you don't really
love JavaScript, you enjoy JavaScript.
So it will rework the string and it will update those values.
So you can set it as well to my string if you wanted to.
So if you could update the current string or you can create a new variable to hold the new value so
that we can get the first index value and we can also get the last index value, the string object,
an index of and looking for the index of.
And in this case, let's do JavaScript, because we do have two of those words in there.
We can see that the starting position of JavaScript is 13, whereas if we want to do last index of this
whole return, the last occurrence of the word, so we just put last in front of index of and there
should be uppercase and refresh and you can see the last index.
So this JavaScript is starting at position forty seven.
So you can use these in combination as well, where you can slice at a word if you want, if you know
it's going to be nine characters and you can get the index starting index value using any one of these
index of or last index, you can also search within the index.
So output is going to be equal to my string and search.
So it executes a search for a pattern between a regular expression and a string object so we can search
for a particular word.
In this case, we just use JavaScript together because it makes a lot of sense within this course.
We're searching and we're getting the same result as we did with index of because it's returning the
index position of that search result.
We're going to look at these in more detail as well later on within the section.
When we look at regular expressions, it don't go anywhere yet with this lesson's not concluded.
I want you to try out the string methods that we've been showing you within this lesson to turn a string
into an array of words, replace the content, update the output of the string.
Have some fun.
Try out the different string methods that we've covered.