标签:substring 返回 ext sub stat temp tin 字符串长度 sys
private static ArrayList getMaxSubString(String s1,String s2)
{
//最大相同子串,s1,s2=min
String max=(s1.length()>s2.length())?s1:s2;
String min=(max==s1)?s2:s1;
ArrayList<String> list =new ArrayList<String >();
//循环短的字符串来做截取
for(int x=0;x<min.length();x++)
{
//两个变量 y从头开始递增
// z从较短字符串长度取值递增
//判断条件为 z在上次循环+1后不等于min长度+1
for(int y=0,z=min.length()-x;z!=min.length()+1;y++,z++)
{
String temp=min.substring(y,z);
if(max.contains(temp)){
//两个字符串 abcdef和ace最长的字符串‘a‘,‘b‘,‘c‘有三个采取list存了一下
list.add(temp);
}
//如果list已经添加了参数直接返回list
if (list.size()>0){return list;}
}
return list;
}
@Test
public void maxsubsting(){
String s1="abjdfhelloshdf";
String s2="dflo";
ArrayList list =getMaxSubString(s1,s2);
System.out.println(list.size());
if (list.size()>0){
for (Iterator iterator =list.iterator();iterator.hasNext();){
System.out.println(iterator.next());
}
}
标签:substring 返回 ext sub stat temp tin 字符串长度 sys
原文地址:https://www.cnblogs.com/SongShupengpeng/p/10268339.html