@[TOC](leetcode 28. 实现 strStr()JavaScript解决)
问题描述
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
代码
var strStr = function(haystack, needle) {
if(needle === '') return 0;
for(let i=0 ; i<haystack.length ; i++){
if(haystack[i] == needle[0]){
if(haystack.substr(i,needle.length) === needle) return i;
}
}
return -1;
};