JS:自定义带"count"参数的indexOf成员
代码如下:
String.prototype.myIndexOf = function(childStr,startInd,count){
if (count == null){
return this.indexOf(childStr,startInd);
}else{
if (count < 0){return -1;};
for(i=count;i>1;i--){
startInd = this.indexOf(childStr,startInd) + 1;
} ;
return this.indexOf(childStr,startInd);
}
}
if (count == null){
return this.indexOf(childStr,startInd);
}else{
if (count < 0){return -1;};
for(i=count;i>1;i--){
startInd = this.indexOf(childStr,startInd) + 1;
} ;
return this.indexOf(childStr,startInd);
}
}
使用方法:
Code
注:第一个参数为要查找位置的子字符串,第二个参数为父字符串中开始查找的位置,第三个参数为要查询的出现次数。