Home > Java > javaTutorial > body text

Detailed explanation of how to find the maximum intersection of two strings in Java

怪我咯
Release: 2017-07-02 10:37:11
Original
2286 people have browsed it

This article mainly introduces the method of java to get the maximum intersection of two strings. It involves Java's string manipulation skills and has certain reference value. Friends who need it can refer to it

The example in this article describes the implementation method of obtaining the maximum intersection of two strings in Java, and shares it with you for your reference. The specific implementation method is as follows:

The code is as follows:

package com.itheima.net;
public class Game13
{
    public static void main(String[] args)
    {
        String s1 = "135adbfg67";
        String s2 = "125dbf59";
        String s3 = s2;
        int begin = 0;
        int end = s2.length();
        int i = 1;
        while (!s1.contains(s3))
        {
            if (end == s2.length())
            {
                begin = 0;
                end = (s2.length()) - (i++);
            }
            else
            {
                begin++;end++;
            }
            s3 = s2.substring(begin, end);
            System.out.println(s3);
            System.out.println("--------");
        }
        System.out.println(s3);
    }
}
Copy after login

The code is as follows:

package com.itheima.net;

public class Game15
{
    public static void main(String[] args)
    {
        String s1 = "135adbfg67";
        String s2 = "125dbf59";
        method(s2, s1);
    }
    public static void method(String max, String min)
    {
        if (max.length() < min.length())
        {
            String s = max;
            max = min;
            min = s;
        }
        String subStr = min;
        for (int begin = 0, end = min.length(), i = 1; !max.contains(subStr); subStr = min.substring(begin, end))
        {
            if (end == min.length())
            {
                begin = 0;
                end = (min.length()) - (i++);
            }
            else
            {
                begin++;
                end++;
            }
            System.out.println(subStr);
            System.out.println("--------");
        }
        System.out.println(subStr);
    }
}
Copy after login

The above is the detailed content of Detailed explanation of how to find the maximum intersection of two strings in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!