Transcript from the "Index" Lesson
[00:00:21] Does anybody know the word for that, a number given to a position? Yeah.
>> It's an array.
>> Okay, so arrays also have these numbers assigned to them. We're gonna talk about arrays a little bit later. So this is something that strings and arrays have in common, is the things inside of them each get a number.
[00:00:39] And those numbers, just as a point of trivia, are called?
>> In chat, they're saying index.
>> Index, exactly, so the number given to each thing inside of a more complex thing [LAUGH] when that thing is ordered is called an index, and plural indices, right? So we often talk about, what is the index of a particular character in a string?
[00:01:45] In other words, if I have the string ALOHA and then in square brackets
, it's asking, what is the first character in the string? And so this is going to give me, any guesses?
>> A, so we said that in our, We said that in our strings, the first character, although it sounds sort of counterintuitive if you're not used to counting from 0.
[00:02:40] So in this case, if we're asking the third character, that's gonna be the character at index 2. So again, we have A is at index 0, L is at index 1, O is at index 2. So if I type in ALOHA, I expect to get and I do get O.
[00:03:49] So let's try it out. What do you think is going to be the value of "ALOHA".indexOf("L")?
>> We would expect 1, and it is indeed 1. Beautiful, now, what about this one, "ALOHA" .indexOf("A"). What do you think it's gonna be?
>> 0 is a good contender.
[00:04:14] We do have an A at index 0 as we saw before, cuz we asked it ALOHA(0). We know that that's A, but there's also an A at the end of the string, which would be index?
>> 4, exactly, so which one?
>> It takes the average.
>> [LAUGH] The average, maybe. Perhaps, let's find out. So the way we can find out, as always, is by tinkering with things in our console, and it indeed returns 0. So indexOf is going to find with the first thing like this that I'm looking for. And we're gonna see indexOf works on arrays too, so we're gonna look at that later.
[00:04:51] But in strings, it's going to return the index of the first appearance of this character in the string. So keep in mind that if you have multiple of the same character, you're only gonna find the first one with indexOf. Now, what if I ask it to tell me the index of a character that's not in the string, like Q?
[00:05:13] For example, if I ask "ALOHA".indexOf("Q").
>> Maybe it's gonna be null. That might make sense. Maybe it's gonna be, Dear Anjana, you don't know what you're talking about. Stop asking me for stuff that doesn't exist, unlikely, cuz how does it know my name? Tracking, no, just kidding.
[00:06:24] It's outside of the string. It's in the negative world. Yes, question.
>> Is this case sensitive?
>> Great question. Is this case sensitive? Let's find out. So for example, instead of asking "ALOHA".indexOf("A") ,or let's use L cuz there's only one of them. Let's ask "ALOHA".indexOf("l"), -1. So the answer to the question, is it case sensitive, is, sure is, yes, indeed.
>> Boolean, nice guess, true or false. It's a yes or no question, right? This string either does or does not contain this other string. So we're gonna get a Boolean out from includes, that tells us, yes, true, this string includes that other string, or false. So in this case, we would expect what value for "ALOHA".includes("HA")?
[00:09:26] I can ask.
>> Like a left.
>> Does it start with this thing? So startsWith is this spell we can use here. Yeah, this is just trivia in case you've already seen this before. So startsWith is another question I can ask of a string that similarly is gonna return a value of what type?
[00:10:17] Some of these spells are gonna come in handy as you're working with strings. Okay, now, if we know that strings can contain other strings, then that means we can also ask for an index not just of a single character, but of an entire substring. And we can actually use our good friend indexOf with a longer string, a substring instead of just a single character.
[00:10:47] So earlier we saw "ALOHA".indexOf("H"), let's say, or index of A, or whatever individual character. We can also ask for the index of a substring, so another string that has multiple characters. So what do you think this is going to give us? Let's go back to our drawing of the indices related to the string.
[00:11:13] What do you think "ALOHA".indexOf("HA") is going to tell me? The question is, where does the substring HA begin in the string ALOHA?
>> So I heard the guess 3. I see some nodding. I can't see if the chat is nodding, but I'm just gonna feel the nods.
[00:11:36] [LAUGH] All right, let's
>> There's 3s the chat
>> There's 3s in the chat. Let's double-check. Indeed, it does. And this is why, to go back to our drawing, I said that sometimes it's helpful to think about these indices not as corresponding to a particular character, as in the green numbers at the top here, but to a particular position in the string.
>> I heard -1.
[00:12:29] Let's find out. Boom, yes. So same rules as for a character that doesn't exist in the string. Even though we have an L and an O in ALOHA, the substring LOL does not exist, so the whole thing returns -1. Cool? So cool. We know numbers corresponding to things inside of a string.
[00:12:53] All right, what else can we do with strings? How about we add them together? [LAUGH] So the plus, plus is a thing we're gonna talk about later. Plus can be used to connect strings together. If I use a + on "ALOHA" + "!", this is going to give me a combined string that sort of smooches the two strings together into a new string that has both of them, just one right after the other.
[00:13:28] So "ALOHA" + "!" is ALOHA!, more enthusiastically. [LAUGH] So this is something we're gonna come back to a little bit later when we talk about operators. But plus is an operator that works on strings to, you can also hear the word concatenate strings together, meaning, squish them together.
[00:13:51] But why am I yelling so much? Why is there so much yelling? And of the Schitt's Creek ladies want to know why I'm raising my voice. And so there's another fun tricks that strings can do, which is, they have a spell called toLowerCase. "ALOHA".toLowerCase is going to give me, any guesses?