JavaScript

Javascript split

Javascript is a scripting or programming language of the web. Strings are an important part of the variables in any programming language. We often need to manipulate string according to our needs. In this article, we will talk about the javascript’s split string method which is used to split the string according to our needs. So, let’s take a look at what is a string and what does split string method do.

The string is just a simple text or characters which can include the alphabets, numbers, or symbols.

Javascript’s split() method is called upon when it is required to split the string into the array of substrings in accordance to the separator that you provide.

Syntax

Let’s have a look at the syntax of the split method.

string.split([seperator][, limit]);

Here, the separator could be a single character using which you would like to split the string.

And the limit is the limit of splits. When the number of substrings becomes equal to the limit, the split() method stops.

Let’s dive in and get a couple of examples done for the understanding of the split() function.

Examples

We suppose a string “Linuxhint is great and working hard to grow.”. Now, let’s try to split the string into arrays of string using the split() method. By providing the “ ” space character as a separator. But, without providing the limit. Later, we will do it with the limit.

linux.split(" ");

As you can see that the split() method has returned the array of substrings, based on the “ ” space.

Now, let’s see what happened if we provide a limit as well.

linux.split(" ", 3)

As you can see that the split() function stopped splitting the string, and it would stop splitting the string when the count is equivalent to the number of substrings.

Now, let’s see if we don’t provide a separator just an empty string, and calls the split() function.

linux.split("");

As you can see that the function has split and returned the array of each character separated.

Now, let’s see if we don’t provide both of the arguments and just calls the split() function.

linux.split();

Great, the split() function has returned an array with a single substring in it that is the whole string

Pro tip

What if we want to split the string on the base of two separators or we want separators as well in the output array of substring? Luckily, there is a solution as well, We can provide regular expressions as a separator as well.

So, let’s see how can we split the string with two separators. Space “ “ character and “i” character

linux.split(/\ |i/);

Alright! It worked great. As we expect it to split.

Now, what if we want to include the separators as well in the array of the substring. We will simply add parenthesis() around the regular expression.

linux.split(/(\ |i)/);

Perfect, as you can see that the separators are also included in the array of substrings.

Conclusion

In this article, we have seen that how can we split a string on the base of a separator provided and how can we apply the limit to the split() function. We came across how we could provide regular expressions in compliance with our needs and requirements. So, have a happy and the best learning of javascript with linuxhint.com.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.