我试图在字符串中找到第一个出现的字母.例如,苹果中的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));
}
Run Code Online (Sandbox Code Playgroud)
它似乎没有返回正确的值.