Java获取两个字符串中最大相同子串

java

@Test

public void testGetSomeStr() {

String a = "sffioafihellosdfvasdf";

String b = "sfhellodesdfs";

String c = getSomeStr(a, b);

System.out.println(c); //=> hello

}

public String getSomeStr(String str1, String str2) {

// 获取两个字符串中的最大值

String maxstr = str1;

String minstr = str2;

if (str2.length() > str1.length()) {

maxstr = str2;

minstr = str1;

}

// 先确定循环多少轮

int lunCount = minstr.length();

for (int i = 0; i < lunCount; i++) {

// 确定每轮循环多少次

for (int x = 0, y = lunCount - i; y <= lunCount; x++, y++) {

if(maxstr.contains(minstr.substring(x,y))){

return minstr.substring(x,y);

}

}

}

return null;

}

以上是 Java获取两个字符串中最大相同子串 的全部内容, 来源链接: utcz.com/z/392502.html

回到顶部