【发布时间】:2014-04-14 14:36:58
【问题描述】:
我目前有一个方法应该接受两个字符串,然后检查一个字符串是否作为子字符串存在于另一个字符串中。它不会同时检查两种方式,因此我将字符串传递给方法的方式决定了在另一种方式中查找的字符串。
目前我收到 stackoverflow 错误。
public boolean checkMatch(String text, String regex, int i){
int regexL = regex.length();
if(i == regexL){
return true;
}
System.out.println(text.charAt(i) + " " + regex.charAt(i));
if(text.charAt(i) == regex.charAt(i)){
return checkMatch(text, regex, i++);
}
else if(text.charAt(i) != regex.charAt(i)){
if(text.substring(1) == ""){
return false;
}
else if(text.substring(1) != ""){
return checkMatch(text.substring(1), regex, 0);
}
}
return false;
}
我正在以我的名字为例进行测试。
@Test public void test_52() {
assertEquals(true, checkMatch("Samual", "mu", 0));
}
控制台溢出后的样子。
S 米
一米
米米
米米
米米
等
我哪里出错了?我迭代错了吗?堆栈跟踪显示它似乎被此行捕获。
return checkMatch(text, regex, i++);
但缺陷点很少是故障点。对不起,文字和代码的墙。
【问题讨论】:
-
那么什么值被传递了?我的意思是,实际 值——它们是什么?为什么这会阻止状态被推进?
-
您也可以将
checkMatch的正文替换为这个单行代码return text.indexOf(regex)==i;
标签: java string substring stack-overflow