Java language programming, to find the maximum substring of two strings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.