package tan; class Test { public static String getMaxSubString(String s1,String s2) { String max = "",min = ""; //得到大的那个子串 max = (s1.length()>s2.length())?s1: s2; //得到小的那个子串 min = (max==s1)?s2: s1; // sop("max="+max+"...min="+min); //外层循环遍历整个小的那个子串 for(int x=0; x<min.length(); x++) { //内层循环遍历? for(int y=0,z=min.length()-x; z!=min.length()+1; y++,z++) { String temp = min.substring(y,z); sop(temp); if(max.contains(temp))//if(s1.indexOf(temp)!=-1) return temp; } } return ""; } public static void main(String[] args) { String s1 = "ab"; String s2 = "cvhellobnm"; sop(getMaxSubString(s2,s1)); } public static void sop(String str) { System.out.println(str); } }
程序输出结果为:
andefc
andef
ndefc
ande
ndef
defc
and
nde
def
efc
an
an
原文:http://blog.csdn.net/u010834071/article/details/37763101