Package Stringtest;public class StringDemo4 {public static void main (string[] args) {String str1 = "Ilikejavaverymuch"; String str2 = "Java is useful"; StringDemo4 sd4 = new StringDemo4 (); Sd4.sop (Sd4.getmaxsubstring1 (str1, str2));} /** * For the maximum of two strings of the same substring, thought: * 1, the short string, in the way of decreasing the length of the substring (CORE) * 2, each gets the substring to determine whether the long string exists in the * */string getMaxSubString1 (String str1, String str2) {string maxstr = "", Minstr = ""; maxstr = Str1.length () > Str2.length ()? Str1:str2;minstr = (Maxstr = = str1)? str2:str1;for (int i = 0; i < minstr.length (); i++) {for (int start = 0, end = Minstr.length ()-I; End <= Minstr.length ( ); start++, end++) {if (Maxstr.contains (minstr.substring (Start, end)) {return minstr.substring (start, end);}}} Return "";} Print string void sop (String str) {System.out.println ("str =" + str + ");}}
Printing results:
str = java
Java language programming, to find the maximum substring of two strings