Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 确定字符的发生次数,而不考虑大小写_Java - Fatal编程技术网

Java 确定字符的发生次数,而不考虑大小写

Java 确定字符的发生次数,而不考虑大小写,java,Java,我是Java新手&我希望能够确定一个字符的出现次数,而不考虑大小写。输入文本。这是我所拥有的,但我是通过使用其区分大小写的indexOf读取的,我不希望区分大小写: public int getNumOfOccurrences(String source, String search) { int count = 0; int prevIndex = 0, curIndex = 0; if (source.length() > 0 && search

我是Java新手&我希望能够确定一个字符的出现次数,而不考虑大小写。输入文本。这是我所拥有的,但我是通过使用其区分大小写的indexOf读取的,我不希望区分大小写:

public int getNumOfOccurrences(String source, String search) {
    int count = 0;
    int prevIndex = 0, curIndex = 0;
    if (source.length() > 0 && search.length()>0) {
        count=-1;
        while (curIndex >= 0) {
            curIndex = source.indexOf(search, prevIndex);
            prevIndex = curIndex + 1;
            count++;
        }
    }
    return count;
}

执行与现在完全相同的操作,但在开始计数之前,请调用
上的
.ToLowerCase()
搜索
参数。很明显,这将使所有内容都使用小写


字符串是不可变的,因此您必须执行
String source=source.ToLowerCase()
,或者如果要保持区别,请将其放在不同的变量中。

关于
搜索
?是什么保证它是小写的?@PM77-1:我加了。以后可以自己编辑,这是正确的评论!谢谢成功了。我试过了,但一定没有改变curIndex=source.indexOf(search,prevIndex);to curIndex=sources.indexOf(搜索,prevIndex);