Archive for May 3, 2007

JavaScript split Method – Create Array From String

Posted in JavaScript, Software, Web, internet, programming on May 3, 2007 by Joey

The JavaScript split method is used to create an array from a string.

var strTest = '1,2,3,4';
var aryTest = strTest.split();
document.write(typeof aryTest);

The preceding example demonstrates that when the split method is applied on a string an array object is created. The split method takes an optional argument: The delimiter on which to base the split. Comma is the default.

var strTest = '1|2,3|4';
var aryTest = strTest.split('|');
document.write(aryTest.length);

In this example the split occurs based on a pipe (|) delimiter, so there will only be three elements in this array:

aryTest[0] = 1
aryTest[1] = 2,3
aryTest[2] = 4