javascript indexofany
javascript indexofany
String.prototype.indexOfAny = function (anyOf /*Array*/, startIndex /*uint*/, count /*int*/) /*int*/ {
startIndex = isNaN(startIndex) ? 0 : startIndex;
if (startIndex < 0) {
startIndex = 0;
}
if (this == null || this == "") return -1;
count = isNaN(count) ? -1 : ((count >= 0) ? count : -1);
var l = this.length;
var endIndex /*int*/;
if ((count < 0) || (count > l - startIndex)) {
endIndex = l;
}
else {
endIndex = startIndex + count;
}
if (anyOf == null || anyOf == undefined) return -1;
for (var i = startIndex; i <= endIndex ; i++) {
var curtxt = this.substring(startIndex, i);
var allindex = [];
for (var j = 0; j < anyOf.length; j++) {
var any = anyOf[j];
var index = curtxt.indexOf('' + any);
if (index > -1) {
return index + startIndex;
}
}
}
return -1;
};
startIndex = isNaN(startIndex) ? 0 : startIndex;
if (startIndex < 0) {
startIndex = 0;
}
if (this == null || this == "") return -1;
count = isNaN(count) ? -1 : ((count >= 0) ? count : -1);
var l = this.length;
var endIndex /*int*/;
if ((count < 0) || (count > l - startIndex)) {
endIndex = l;
}
else {
endIndex = startIndex + count;
}
if (anyOf == null || anyOf == undefined) return -1;
for (var i = startIndex; i <= endIndex ; i++) {
var curtxt = this.substring(startIndex, i);
var allindex = [];
for (var j = 0; j < anyOf.length; j++) {
var any = anyOf[j];
var index = curtxt.indexOf('' + any);
if (index > -1) {
return index + startIndex;
}
}
}
return -1;
};
var str = "1234567890a";
window.alert(str.substring(0,1));
window.alert(str.indexOfAny(["a", "21"], 0, 11));
//window.alert(str.indexOfAny(["a", "11"], 0, 11));