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