gpt4 book ai didi

Java 内置方法 - 字符串中字符的出现次数

转载 作者:行者123 更新时间:2023-12-02 06:08:03 25 4
gpt4 key购买 nike

Java 是否有内置方法来计算字符串中字符的出现次数?例如:s=“你好,我叫乔尔”l 出现的次数为 3

谢谢

最佳答案

没有这样的方法,但你可以这样做:

String s = "Hello My name is Joel";
int counter = 0;
for( int i=0; i<s.length(); i++ ) {
if( s.charAt(i) == 'l' ) {
counter++;
}
}

(代码来自Simple way to count character occurrences in a string)

关于Java 内置方法 - 字符串中字符的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22117081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com