Using String split() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the string technique known as split() with syntax and examples.

Description

In JavaScript, split() is a string approach that is used to break up a string into an array of strings using a particular delimiter. Because the split() approach is a technique of the String object, it ought to be invoked via a particular occasion of the String class.

Syntax

In JavaScript, the syntax for the split() approach is:

string.split([delimiter [, count]]);

Parameters or Arguments

delimiter Optional. It is delimiter used to destroy the string into the array of substrings. It can be a single character, string or regular expression. If this parameter is not provided, the split() approach will return an array with one issue containing the string. limit Optional. It determines the number of instances the split will be carried out the usage of the delimiter. Once the limit has been reached, the split() technique will discard the remainder of the string and it will not be lower back as section of the array.

Returns

The split() approach returns an array of strings that have been break up aside the use of the precise delimiter. The delimiter itself is no longer included in the resulting array elements.

Note

The split() method does now not change the price of the unique string. If the delimiter is an empty string, the split() approach will return an array of elements, one aspect for each persona of string. If you specify an empty string for string, the split() method will return an empty string and now not an array of strings.

Example

Let’s take a seem to be at an instance of how to use the split() technique in JavaScript.

For example:

var totn_string = 'Tech On The Net';

console.log(totn_string.split(' '));
console.log(totn_string.split(' ', 2));

In this example, we have declared a variable called totn_string that is assigned the string cost of ‘Tech On The Net’. We have then invoked the split() approach of the totn_string variable to spoil the string into an array of strings the usage of a delimiter.

We have written the output of the split() technique to the web browser console log, for demonstration purposes, to exhibit what the split() technique returns.

The following will be output to the net browser console log:

["Tech", "On", "The", "Net"]
["Tech", "On"]

In the first call, we used a house persona as the delimiter. In this example, the split() approach lower back an array of four elements consisting of the strings ‘Tech’, ‘On’, ‘The’, and ‘Net’.

In the second call, we detailed a restrict of two as nicely as a space delimiter character. This time, the split() approach lower back an array of two elements consisting of ‘Tech’ and ‘On’ and discarded the remainder of the string after the limit of two splits was once reached.

Using a Regular Expression as a Delimiter

Finally, we are going to take a seem at how to break up a string into an array of strings using a everyday expression.

For example:

var totn_string = 'Tech On The Net';

console.log(totn_string.split(/\s+/));

The following will be output to the web browser console log:

["Tech", "On", "The", "Net"]

In this example, we have used a ordinary expression for the delimiter so that we can split the string into words delimited via spaces. In this example, the split() method will return an array of 4 elements consisting of the strings ‘Tech’, ‘On’, ‘The’, and ‘Net’.