JavaScript split Method

The JavaScript split method is used to split a string into an array of sub strings , and returns the new array.
Note: The JavaScript split method does not change the original string.

Syntax:

string.split(separator,limit)

Where,
separator  = Optional. Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item)

limit = Optional. An integer that specifies the number of splits, items after the split limit will not be included in the array

Example:

var str=”This is the JavaScript split string method!”;
var n=str.split();
alert(n);              // OUTPUT: This is the JavaScript split string method!

n=str.split(“”);
alert(n);              // OUTPUT: T,h,i,s, ,i,s, ,t,h,e, ,J,a,v,a,S,c,r,i,p,t, ,s,p,l,i,t, ,s,t,r,i,n,g, ,m,e,t,h,o,d,!

n=str.split(” “);
alert(n);              // OUTPUT: This,is,the,JavaScript,split,string,method!

 

Live Demo of JavaScript split method

 

Leave a Reply