StrStr,判断一个字符串是不是另一个字符串的字串,并返回子串的位置
1 public int strStr(String haystack, String needle) { 2 3 if(haystack == null || needle == null) 4 { 5 return -1; 6 } 7 8 for (int i = 0; i <= (haystack.length()-needle.length()); i++) 9 { 10 int m = i; 11 for (int j = 0; j < needle.length(); j++) 12 { 13 if (needle.charAt(j) == haystack.charAt(m)) 14 { 15 m++; 16 if((m-i) == needle.length()) 17 { 18 return i; 19 } 20 } 21 else 22 { 23 break; 24 } 25 } 26 } 27 return -1; 28 }