Click to See Complete Forum and Search --> : explode in javascript


dotku
04-29-2007, 05:02 PM
first read the code below

function explode(inputstring, separators, includeEmpties) {
inputstring = new String(inputstring);
separators = new String(separators);

if(separators == "undefined") {
separators = " :;";
}

fixedExplode = new Array(1);
currentElement = "";
count = 0;

for(x=0; x < inputstring.length; x++) {
char = inputstring.charAt(x);
if(separators.indexOf(char) != -1) {
if ( ( (includeEmpties <= 0) || (includeEmpties == false)) && (currentElement == "")) { }
else {
fixedExplode[count] = currentElement;
count++;
currentElement = ""; } }
else { currentElement += char; }
}

if (( ! (includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
fixedExplode[count] = currentElement; }
return fixedExplode;
}


i found some problem
when i use it, it can't customize the array
demo http://www.dotku.net/lab/js/expload.html

var pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
document.write(pieces = explode(" ", pizza, 3));
document.write("<br/>"+pieces);

there should be only 3 item in the array, but it's 6~!