AppDividend
Latest Code Tutorials

Javascript String Split Example | String.prototype.split() Tutorial

0 51

Get real time updates directly on you device, subscribe now.

Javascript String Split Example | String.prototype.split() Tutorial is today’s topic. The javascript string split() method splits the String object into an array of strings by separating a string into substrings, using a specified separator string to determine where to make each split. The string split function is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. The javascript string split() method returns a new array. At each match of the separator .split() method will split the string and add the split section as an element to an array.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript String Split Example

If an empty string (”) is used as the separator, the string is split between each character. The string split() method does not change an original string. The syntax for the split() method is following.

string.split(separator, limit)

The separator is an optional parameter. The separator specifies a character, or a regular expression, to use for splitting a string. If omitted, then the entire string will be returned.

The limit is an optional parameter. It is an integer that specifies the number of splits, items after the split limit will not be included in the array.

Let us take a simple example of string split in javascript.

// app.js

let str = 'Obama was US president';
let result  = str.split('was');
console.log(result);

So, in the above example, we have split the string from a was separator. So the output looks like this.

 

Javascript String Split Example | String.prototype.split() Tutorial

When found, the separator is removed from the string, and the substrings are returned in an array. If the separator is not found or is omitted, the array contains one element consisting of the entire string. If the separator is an empty string, a string is converted to an array of characters. If the separator appears at the beginning or end of a string, or both, then the array begins, ends, or both begins and ends, respectively, with the empty string. Thus, if a string consists solely of one instance of the separator, the array consists of two empty strings. If the separator is an array, then Array is coerced to String and used as a separator.

Returning a limited number of splits

Look at the following example.

// app.js

let str = 'Obama was US president';
let result  = str.split(' ', 3);
console.log(result);

 

Javascript String Split Example

In the above example, we have set the limiter 3. So it will split and returns three elements of an array.

Split with a regular expression.

// app.js

let str = 'Obama was US president';
let result  = str.split(/(\d)/);
console.log(result);

 

String.prototype.split() Tutorial

Finally, Javascript String Split Example | String.prototype.split() Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.