class Test{
public static void main(String[] args) {
String a="abcwerthelloadcedf";
String b="cdhelloesadcedf";
String c=getSonString(a,b);
System.out.println(c);
}
private static String getSonString(String a, String b) {
String max=null;
String min=null;
String temp="";
if (a.length()>b.length()) {
max=a;
min=b;
}else {
max=b;
min=a;
}
for (int i = 0; i <min.length(); i++) {
for (int j = 0; j <=i; j++) {
String substring = min.substring(j, min.length()-i+j);//从小的字符串开始,从左向右移位比较
if (max.contains(substring)) {
if(temp.length()<substring.length()) {
temp=substring;
}
}
}
}
return temp;
}
网友评论