Skip to content

Instantly share code, notes, and snippets.

@twaddington
Created May 26, 2011 19:26
Show Gist options
  • Save twaddington/993859 to your computer and use it in GitHub Desktop.
Save twaddington/993859 to your computer and use it in GitHub Desktop.
Search a string for a substring
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