Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 寻找最长重复子串_Java_String - Fatal编程技术网

Java 寻找最长重复子串

Java 寻找最长重复子串,java,string,Java,String,可能重复: 我想找到字符串中最长的重复子字符串 /** This method will find the longest substring of a given string. String given here is reassuring. */ public String longestRepeatedSubstring() { String longestRepeatedSubstring = ""; for (int i = 0; i<text.

可能重复:

我想找到字符串中最长的重复子字符串

    /**
This method will find the longest substring of a given string.
String given here is reassuring. 

 */
public String longestRepeatedSubstring()
{
    String longestRepeatedSubstring = "";
    for (int i = 0; i<text.length(); i++ )
    {
        String one = text.substring(0,i); 

        for(int o = 0; o<text.length();o++)
        {
            Sting two = text.substring(0,o);
            if(one.equals(two))
            {
                longestRepeatedSubstring = one;
            }

        }

    }
    return longestRepeatedSubstring; 
}

你最好使用正则表达式。这是:

/(?=((.+)(?:.*?\2)+))/s

如果您的问题没有得到回答,请尝试回复评论,不要再发布。我无法找出我的代码有什么问题。我正在寻找字符串中最长的子字符串。我在你的帖子上没有看到任何问号;实际问题在哪里?您是否尝试使用调试器对其进行调试?您的方法返回整个字符串,对吗?