求两个字符串中的最大相同子串 SubString

简介:

package com.itcast.base;

public class SubStringTest {
 public static void main(String[] args) {
  String str1 = "ashfjeudccckfjgiccccccjgurhd";
  String str2 = "dhfurjcccckgoymjdhccfi";
  
  String max = "";
  for (int i = 0; i < str2.length(); i++) {
   String temp1 = str2.substring(i);
//   System.out.println("temp1:" + temp1);
   for (int j = temp1.length() - 1; j >= 0; j--) {
    String temp2 = temp1.substring(0, j);
//    System.out.println("temp2:" + temp2);
    if (str1.indexOf(temp2) != -1 && temp2.length() > max.length()) {
     max = temp2;
    }
   }
  }
  System.out.println("max:" + max);
 }
}

相关文章
|
1月前
|
前端开发 JavaScript
str.repeat(2)将字符串重复两次,let str_trim = ‘1 1 1 1‘str_trim.trim() 将字符串的空白部分去掉,toLowerCase()小写,toUpperCas
str.repeat(2)将字符串重复两次,let str_trim = ‘1 1 1 1‘str_trim.trim() 将字符串的空白部分去掉,toLowerCase()小写,toUpperCas
|
3月前
|
存储 编译器 C++
c++字符串
c++字符串
27 0
|
8月前
|
C++
C/C++判断字符串是否为另一字符串的子字符串
C/C++判断字符串是否为另一字符串的子字符串
96 0
|
8月前
|
C++
C++中从一个字符串中截取另一长度的子字符串
C++中从一个字符串中截取另一长度的子字符串
41 0
|
测试技术 索引
根据首尾字符串截取中间字符串
今天分享一个函数:虽然它非常简单,但是真的很好用!也很常用!比如 “我今天真的很高兴” 这句话,要把 `今天` 截取出来,我们可以直接调用函数拿到结果,不需要匹配索引、也不用写正则!
64 0
字符串截取
字符串截取
45 0
7-121 删除字符串中的子串
7-121 删除字符串中的子串
53 0
Zp
截取字符串substring与substr之间的区别
截取字符串substring与substr之间的区别
Zp
146 0