Skip to content

Instantly share code, notes, and snippets.

@z2015
Created July 4, 2018 07:15
Show Gist options
  • Save z2015/9cb75c33d98432973708df0b70d9deb6 to your computer and use it in GitHub Desktop.
Save z2015/9cb75c33d98432973708df0b70d9deb6 to your computer and use it in GitHub Desktop.
String IndexOf example
function strIndexOf(val, str) {
val = String(val);
var strLen = str.length;
if (strLen >= val.length) {
var head = val[0]
var hasHead = -1;
var matchLen = 0;
for (var i = 0; i < strLen; i++) {
var v = str[i];
if (hasHead >= 0) {
if (v === val[matchLen - 1]) {
matchLen++;
} else {
hasHead = -1;
matchLen = 0;
}
} else {
if (head == v) {
hasHead = i;
matchLen = 1;
}
}
if (matchLen === val.length) {
return hasHead;
}
}
}
return -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment