题意:在一个字符串中查找是否存在子串和其他的字符串相同
思路:暴力破解
代码:
package ImplementStr;
public class ImplementStr {
public String FindStr(String haystack , String needle){
int m = haystack.length();
int n = needle.length();
if(m < n || m == 0) return null;
for(int i =0 ; i < m ; i++){
int k = i;
int j = 0;
while(j < needle.length() && k < haystack.length() && haystack.charAt(k)== needle.charAt(j)){
k++ ;
j++ ;
if(j == n) return needle;
}
}
return null;
}
public static void main(String[] args) {
ImplementStr isStr = new ImplementStr();
String haystack = "i love java";
String needle = "java";
String string = isStr.FindStr(haystack, needle);
System.out.println(string);
}
}