实现 strStr() 函数。
给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
public static int strStr(String haystack, String needle) {
int len = needle.length();
if(len==0) return 0;
if(haystack.equals(needle)) return 0;
for (int i = 0; i < haystack.length(); i++) {
String s = "";
int j = i;
//防止下标溢出
for (j = i; j < len+i && j<haystack.length(); j++) {
s += haystack.charAt(j);
}
if(needle.equals(s)){
return i;
}
}
return -1;
}