获取两个字符串中最大相同的子串,为何我运行结果是“maxsub=cctvo
”为什么不是cctv这个字符串呢
public class StringTest1 {
/**
* @param args
*/
public static void main(String[] args) {
String s1 = "erecctvwwqs";
String s2 = "cctvo";
String maxsub =getMaxSubstring(s1,s2);
System.out.println("maxsub="+maxsub);
}
public static String getMaxSubstring(String s1,String s2){
for(int x=0;x<s2.length();x++){
for(int y=0,z=s2.length()-x;z!= s2.length()+1;y++,z++){
String temp = s2.substring(y,z);
//System.out.println(temp);
if(s1.indexOf(temp)!=1);{
return temp;
}
}
}
return null;
}
}
|