小编use*_*026的帖子

使用递归来查找字符串中的字符

我试图在字符串中找到第一个出现的字母.例如,苹果中的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)

它似乎没有返回正确的值.

java string recursion

4
推荐指数
2
解决办法
5148
查看次数

标签 统计

java ×1

recursion ×1

string ×1