Created
July 4, 2018 07:15
-
-
Save z2015/9cb75c33d98432973708df0b70d9deb6 to your computer and use it in GitHub Desktop.
String IndexOf example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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