I have string as this is test for alternative. What I want to find is location of for. I know I could have done this using alert(myString.indexOf("for")), however I don't want to use indexOf.
Any idea/ suggestion for alternative?
Again, I need this done by Javascript only. No jQuery.. sadly :(
.search()?
"this is test for alternative".search("for")
>> 13
You could code your own indexOf ? You loop on the source string and on each character you check if it could be your searched word.
An untested version to give you an idea:
function myIndexOf(myString, word) {
var len = myString.length;
var wordLen = word.length;
for(var i = 0; i < len; i++) {
var j = 0;
for(j = 0; j < wordLen; j++) {
if(myString[i+j] != word[j]) {
break;
}
}
if(j == wordLen) {
return i;
}
}
return -1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With