Posts Tagged ‘strings’

Exploding a string into an array in Javascript.

// December 18th, 2003 // No Comments » // web development

Found the function below at webreference. It will take a string and explode it into an array. Neato huh?

In my project, I had one field in a form where the user would enter several email addresses seperated by semi-colon’s. I wanted to validate that each email address was valid, but first you have to seperate the long string.

function explodeArray(item,delimiter) {
tempArray=new Array(1);
var Count=0;
var tempString=new String(item);
while (tempString.indexOf(delimiter)>0) {
tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
Count=Count+1
}
tempArray[Count]=tempString;
return tempArray;
}