【发布时间】:2023-01-02 15:46:44
【问题描述】:
public int countCode(String str) {
int code = 0;
for(int i=0; i<str.length()-3; i++){
if(str.substring(i, i+2).equals("co") && str.charAt(i+3)=='e'){
code++;
}
}
return code;
}
大家好,我已经通过互联网的一些帮助解决了这个问题。但我面临的实际问题是 for 循环中的 (str.length()-3) 。我不明白为什么 str.length()-3 中有这个 -3。请解释一下...
【问题讨论】:
-
因为
str.charAt(i+3)。如果您不使用 3`,您将获得StringIndexOutOfBoundException -
嗨...但是,我问的是 for 循环条件 str.length()-3。为什么会这样,-3 的目的是什么。 -3 是如何在我的代码中生效的……请解释一下……
标签: java string underscore-java