Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          All right then, my friends.

          So we've already seen a few basic string methods in the last video.
          
          And remember, methods are just functions that belong to a specific object or object type.
          
          Now let's have a look at some more methods.
          
          So first of all, I'm going to create a new variable called email and set that equal to Mario at the
          
          net Ninja Co.uk.
          
          And let's spell this correctly.
          
          Okay, So now let's use some methods on this variable right here.
          
          So the first one I'm going to show you is called Last Indexof.
          
          Now I'm going to store the results in a variable called Result and set this equal to email dot last
          
          index of.
          
          So we're using this method on the email, which is the string.
          
          And this method finds us the last index of a particular character in that string.
          
          So for example, say I want to find the last instance of, I don't know, something like N Then we pass
          
          that in there as a string, right?
          
          The string is the argument that's going to find us the position of the last n it comes across inside
          
          this string, which is this.
          
          So the position of this n So now let's come down here and say console dot log a result just to see if
          
          this works.
          
          And we'll preview this in a browser and we can see 14 Let's just quickly count 012, three, four,
          
          five, six, seven, eight, nine, ten, 11, 12, 13, 14.
          
          Spot on.
          
          Okay, so that works.
          
          All right, let me comment this out and let's move on to the next one.
          
          So next, I'd like to do a method called slice.
          
          And what slice does is essentially slice a section from the string.
          
          So let's do this.
          
          We'll say let result this time equal to the email, which is the string dot slice.
          
          And inside we pass in two arguments.
          
          This time not just one like we have been doing up here now two arguments.
          
          Now the first argument is where we want to slice from.
          
          So if we want to slice from the beginning, we say position zero.
          
          The next argument is where we want to slice to.
          
          So if we want to slice to position ten, we say position ten.
          
          And if we save that, we're still logging out the result, which is this over here.
          
          So let's save it view in a browser and we can see this.
          
          Okay, so it slices that section from zero to position ten and it logs that to the console.
          
          Now when we do it here, so let's change this to five and we should just get the first five letters,
          
          which is Mario Cool.
          
          So remember, the first argument is where we go from.
          
          The second argument is where we go to, okay, so we could do this if we wanted to from 2 to 5 and we
          
          get something like real.
          
          All right then.
          
          So the next one I'd like to show you is similar to slice and we'll set result equal to this.
          
          So let result equal to email dot sub str.
          
          So sub string.
          
          And what we're going to be doing with this is making a sub string from this.
          
          It's very much like slice, but the two arguments represent something slightly different.
          
          So the first one is still the start position.
          
          So if I say zero, I'm saying start from position zero.
          
          But this time the second argument is how many characters we want to go along.
          
          So if I say ten, then it's going to get us the first ten, right?
          
          Which is the same as 0 to 10 here.
          
          But if I say 4 to 10, then it's going to go from position four and it's going to carry on 14.
          
          So I'm going to save this now and let's view this in a browser and now we can see it goes from position
          
          four up to position 14, it gets ten characters.
          
          Okay.
          
          So that's the difference between these two methods.
          
          Okay.
          
          So let's move on to the next one.
          
          I'll say let results equal to email and this time we'll use a method called replace.
          
          Now what replace does is replace a certain character in the string with another character.
          
          So it takes two arguments.
          
          I'm going to say M And if I pass in the second argument to be W, then what it's going to do is find
          
          the first M inside this string and replace it with a W.
          
          Now the first M is right here, so let's save this and test it.
          
          And we can see now we get Wario at the Net Ninja so it's replaced that first M Cool.
          
          Okay then.
          
          So let's do another example of this method.
          
          What I'm going to do is say this time let results equal to email dot replace and we're going to replace
          
          this time N with W.
          
          So what do you think is going to happen here?
          
          Because we have several ends.
          
          We have this one, this one and this one.
          
          Well, if I save it, let's have a look and we can see Mario at the wet ninja.co.uk.
          
          So it's only replaced the first N right here.
          
          So that's what this replace function does when we just pass it in a simple string like this, it replaces
          
          the first one it comes to with this.
          
          We can use this with regular expressions to do something a little bit different.
          
          But since we've not touched on those yet, we'll leave that for later.
          
          Now, there are some other string methods that I do want to show you, but we're going to be looking
          
          at those as we carry on through the course.