split()

The split() method is used to string which it is used with into separate strings, each separated by the separator (see syntax). If a regular expression is used, the expression must be valid and find a match for anything to be returned by the split method. The results are returned in a zero based array, each item of which contains the string, now split accordingly.

syntax:

string.split(separator, number)

string.split(separator)

string.split(regular expression, number)

string.split(regular expression)

EXAMPLE

var stringToSplit = new String("This is the split string");

var stringSeparator = " ";

document.write(stringToSplit.split(stringSeparator) );

The example shows the creation of a new string, stringToSplit. The stringSeparator is then used to specify that the string is to be separated with the default separator, which is a comma. The contents are then written to the screen through the use of a document.write statement with the split() method invoked. The separator stringSeparator is within the brackets of the split() method as the argument. The output should look like the following:



This,is,the,split,string