本帖最后由 途中ms前进 于 2015-5-25 21:12 编辑
获取两个字符串中最大相同子串。
class StringTestDemo
{public static void main(String[] args)
{String s1="abcdiopjgfdbn";
String s2="abcdryuhfds";
sop(getMaxSubSring(s1,s2));
}
public static String getMaxSubSring(String s1,String s2)
{String max="",min="";
max=(s1.length()>s2.length())?s1:s2;
min=(max==s1)?s2:s1;
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);
if(max.contains(temp))
return temp;
}
}
return "";
}
public static void sop(String str)
{
System.out.println(str);
}
}
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.lang.String.substring(String.java:1951)
at aaa.Test5.getMaxSubSring(Test5.java:20)
at aaa.Test5.main(Test5.java:7)
|
|