【发布时间】:2014-05-16 08:53:05
【问题描述】:
我正在尝试查找字符串中第一次出现的字母。例如,苹果中的 p 应该返回 1。这是我所拥有的:
// Returns the index of the of the character ch
public static int indexOf(char ch, String str) {
if (str == null || str.equals("")) {
return -1;
} else if(ch == str.charAt(0)) {
return 1+ indexOf(ch, str.substring(1));
}
return indexOf(ch, str.substring(1));
}
它似乎没有返回正确的值。
【问题讨论】:
-
你有什么理由不用
String.indexOf(int ch)? -
@LutzHorn 作业 - 愿意下注吗? :)