我怎么能有一个递归 boolean 方法来检查它是否包含 bab。我有这个,但我想递归地做。
public static boolean hasBAB(String str) {
if (str.length() < 3) return false;
if (str.substring(0,3).equals("bab"))
return true;
else
return false;
}
最佳答案
public static boolean hasBAB(String str) {
if (str.length() < 3) return false;
if (str.substring(0,3).equals("bab"))
return true;
else
return hasBAB(str.substring(1));
}
但是你真的应该使用String.contains
关于java - 包含 "bab"的 boolean 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593825/