To split a string in javascript, we use split() method which splits the string into an array of substrings.
Syntax: string.split(separator);
Where separator specifies the character(s) to use for separating the string, and if this separator ( or delimeter) is found in the main string , separator is removed from that string and an array is return consisting of substrings. If separator is not found, the array contains one element consisting of the entire string.
Eg: Below is the function for using split() method:
function splitString() {
var strToSplit = "My Split Program.";
var separator=" ";
var strResult = strToSplit.split(separator);
for(var i=0;i<strResult.length;i++)
{
alert(strResult[i] );
}
}
Output of above function:
strResult[0]= My
strResult[1]=Split
strResult[2]=Program.
0 Comment(s)