در حال بارگذاری ...

Checking String Prefixes0:00

All right, all right, so this lesson isn't really fancy, it's not going to blow you away, but there's some new additions to the string API that you'll get a ton of use out of. Okay, so imagine this, imagine you have a title, we'll use this book I keep hearing about on Twitter, everyone's reading this, I need to check it out. Anyways, maybe we just want to check, does the string begin with red? Okay, one of the ways we could do it in the past is something like this, you check the indexOf, and you say, well, let's just see if it begins with an R. If the indexOf R equals zero, well, we could say R is the first character. So let's try it out, let's bring back the output, and you can see it logged here. Okay, so yeah, that's kind of how we did it, or let's see if it starts with blue, or if

Using startsWith and endsWith2:07

You check the index, and you see if that position is the first one. It's the zero index. But now, much better. We just say, if the string starts with red, then proceed. So much better. We run it. You don't see anything because it's not the case. But if we fix that, now it is the case. Now we can also do endsWith. So for example, if it endsWith ing, we'll just say, it does, something like that.

Offset Argument for Methods2:27

Now we can also do endsWith. So for example, if it ends with ing, we'll just say, it does, something like that. And you can see that will work as well. All right, so we've learned three new methods: String.includes, String.startsWith, and then finally, String.endsWith. Now you can add a second argument, which sometimes may be useful. And this would generally refer to the starting index position. So for example, I could say, startsWith i, and then we offset it. So 0, 1, 2, 3, 4, 5. Okay, well if we offset it by that much, and we run it, you'll see that in that case, it

Repeating Strings with repeat3:32

Kind of weird there, but mostly, you just want to know the basic methods here. Now here's another one. We could do string.repeat. You may be familiar with this from a number of languages, like Ruby. Very useful. So imagine we have, once again, some kind of string, and we will make that equal to lol, and then we want to say string.repeat 100 times, and then if we console.log it, and run it, there you go. So very obvious. You take the string, and you're going to repeat it however many times you pass in here.

Formatting Output with repeat4:51

Anyways, what else can we do with repeat? Maybe a fancy title or something like that. So how about this? Maybe we have some kind of heading. Maybe you log it in the terminal or something, and you want it to look a little more fancy. Okay, well, the heading will be, the heading is here. And yes, we can console.log that, and we'll spit that out. But yeah, we want to format it a bit nicer. So what if we instead said console.log and =, and we will repeat that five times, + the heading itself, + once again, =.

Get off my back. Maybe we do this instead, let padding, we're just taking this way too far. Let padding equals, let's hide this, and equal sign repeated 10 times, and then we could change this to the pattern, then the heading, and then the pattern again. And I'm sorry, we named that pattern. Okay, so we run it again, and now you get it. We're not doing any more examples, let's be done with it. So you've learned about string.includes, string.startsWith, string.endsWith, and string.repeat in this lesson. Incidentally, in ECMAScript 7 or 2016 or whatever they want to call it at this point,

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟