【发布时间】:2013-11-12 17:23:39
【问题描述】:
谁能告诉我为什么这个 wordsearch 方法不起作用 - count 的返回值在我每次运行时都是 0。
public int wordcount(){
String spaceString = " ";
int count = 0;
for(int i = 0; i < this.getString().length(); i++){
if (this.getString().substring(i).equals(spaceString)){
count++;
}
}
return count;
}
getString 的值 = 我的搜索字符串。 如果有人可以提供帮助,我们将不胜感激 - 我敢肯定我正在做一些愚蠢的事情。 迪伦
【问题讨论】:
-
最好使用以空格为分隔符的字符串拆分方法,并检查该数组的长度。
-
你需要一个开始位置和一个结束位置才能通过子字符串获取一个char。
标签: java string for-loop substring wordsearch