Created
May 26, 2011 19:26
-
-
Save twaddington/993859 to your computer and use it in GitHub Desktop.
Search a string for a substring
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 search(str1, str2) { | |
var i,n; | |
for (i=0; i<str2.length; i++) { | |
for (n=0; n<str1.length; n++) { | |
var c1 = str1.charAt(n); | |
var c2 = str2.charAt(i+n); | |
if (c1 === c2) { | |
if (n === (str1.length-1)) { | |
return i; | |
} | |
} else { | |
break; | |
} | |
} | |
} | |
} | |
search('bcd', 'abcdcdef'); // returns 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment