How to Split a String in JavaScript
Learn how to split a String in JavaScript. The split method is available on JavaScript strings and it takes 2 optional arguments - separator and limit. Using these 2 arguments, you can serve a lot of use cases involving splitting of a string.
Code
'use strict';let s = "I love JavaScript"let splitResult = s.split("love")let s = "I love JavaScript"let splitResult = s.split("like")let s = "bonsaiilabs"let splitResult = s.split()let s = "|JavaScript"let splitResult = s.split("|")let s = "|JavaScript|"let splitResult = s.split("|")let s = "bonsaiilabs"let splitResult = s.split("")let s2 = "bonsaiilabs,british columbia,BC,Canada"let splitResult = s.split(",", 2)let splitResult = s.split(",", 0)let splitResult = s.split(",")let splitResult = s.split(",", -1)
Full Transcript
0:00 Hello there, it's Harit from bonsaiilabs. In today's JavaScript session, we will learn how to split a string in JavaScript using various examples. So let's dive in. We're going to start with a new string which says "I love JavaScript", and then we are going to create a variable called splitResult, which will contain the result of s.split and the input to split as love as separator and finally we are going to print the result onto the console. Let's run it. The result is an array that contains two elements - I which exist before love and JavaScript that exist after love in the original string. Now what happens if the separator does not exist in the original string? So let's say change it to like and I cleared the screen and run it. The result contain an array that consists of one element with the entire string as the result.
0:57 Now I will change the input string to bonsaiilabs. And what happens if I do not provide any argument to split method. As I clean and run, you would see that the returned array consists of one element consisting of the original string bonsaiilabs. Now I will change the input string to JavaScript starting with the vertical bar character, and I will split based on the vertical bar character. Now as a clean and run, you would see that the resulting array consists of an empty string at the start. If the separator exists at the end of the string, we will clean and run to confirm that the resulting array consist of the empty string as is the last element of the array. If the separator exists both at the start, at at the end of the string, the resulting array will consist of empty strings as the first and the last element of the result array.
1:49 Now I will change the input string to bonsaiilabs. And what happens if the separator is an empty string? As we run, we will see that the entire string has been converted into the array of characters. We'll clear the screen and this time we're going to use a string that contains multiple commas and use comma as the separator for the split method. As we run, we will see that our result contains four elements in an array. Each element created by using comma as the separator. Now you can limit how many results you want by passing in the second argument to split and giving a positive number. As we run, you would see that the result only contains first two results of the split. Since the second argument is number, how about we change it to zero and see what happens. So when we clean and run you would see when the number is zero, no split happens and the result is an empty array. And what happens if we change this number to be a negative number, let's say minus one. In that case, when you clean and run, you would see that split performs the complete split. It disregards the negative number because it expects a positive number to be part of the limit. And That's it. If you like the video, consider subscribing us to receive more engaging content about JavaScript every single week. Until then, keep learning new skills and apply them regularly to become an expert programmer. See you next time.