Loading
Lesson 24
Courses / JavaScript for Complete Beginners
Using split to Break Down a String into an Array in JavaScript

Video Transcript

Welcome to NBK Tech World. In this lesson we're going to make a connection between the string type and the array type in JavaScript. We're going to learn how to take a string of characters and split it into an array of substrings. We're going to learn the split method. So to get started let's do an example. Say we have a string that holds a sentence. Following sentence there is a cat on the table. Period. So this is a string. Let's store this string in a variable. I am going to use the lat keyword just in case I need to change this variable later on. So the variable is going to be called sentence and hold this string. Okay so let's just make sure the sentence string is there. Now let's say I want to capture each of these words separately in a list, an array. The way we have it right now it's inside the string. The whole string contains the sentence. It's one single entity. But what if you want to grab each of those words separately? So what we're going to use is the split method. So we're going to say the following. Take the variable name dot split. Split is a method in this case of a string. Okay type. So the split expects in this case an argument that's going to be the separator. You have to tell it how you want to split this string. In this case what's the separator here? What separates each word from the other? In this case it's the white space, right? So the white space is the separator. So we're going to write the white space as a string. This is the white space. Okay there's nothing but there is something. It's the white space, the space bar. And that's our separator. Now see what is going to happen. So it takes the string with the separator. It's going to split the sentence into multiple strings in a list in an array. Each element of this array is going to be the word in the sentence. There is a cat on the table. Notice the period is included because the separator is the space and it does not care if it's a word or not. It includes everything that's in between the space and the other space. In this case the end of the sentence so it doesn't have anything else to capture. So there you go. This is the split. And this creates an array of seven elements. Okay. And notice it's not necessarily by word because the period dot is there. Okay. Now there are other ways you could split this sentence. You could split any character you want. So for example sentence dot split. Let's split this into using the character a. So you're telling it to whenever you see an a you're going to take that and break the string right at the point where the a is. And it does not include the separator character. So let's see what we get. So it captures everything. There is space and then it finds the a. The a is the separator. So it takes breaks right here. It removes the separator character and goes to the next element. Keeps going space cat and then it hits the a again. Right. And the a is the separator is removed. You get a break right there. Then it makes a new element and keeps going. T on the T a got the a right here. Let's make a barrier here. Make a wall and split into different substrings and finally bowl with the period. Okay. So that's the split method of the string. The argument is a separator. Now.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: