public class 獲取兩個字符串中最大的相同子串 { 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; } }