js的缺点之一就是函数库太少,自己搜了很多函数,其中这个explode很有用,在java也好,php也好,都有类似的分割字符串为数组的方法,不过要是想在js中用,那么只能自己手写了。
下面这个函数是较好用的一个:
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++) {
str = inputstring.charAt(x);
if(separators.indexOf(str) != -1) {
if ( ( (includeEmpties <= 0) || (includeEmpties == false)) && (currentElement == "")) {
}
else {
fixedExplode[count] = currentElement;
count++;
currentElement = "";
}
}
else {
currentElement += str;
}
}
if (( ! (includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
fixedExplode[count] = currentElement;
}
return fixedExplode;
}