请问如何在一个字符串中查询一个字符出现的次数
例如:字符串asdfasf 我想查询a出现的次数?
------解决方案--------------------循环统计就是了,还有网络上有很多现成的算法,拿来用就是了
------解决方案--------------------int count = -1, lastIndex =0;
while(lastIndex> =0){
count ++;
lastIndex = str.indexOf( 'a ', lastIndex);
}
------解决方案--------------------好象集合类有一个方法可以,我不太记得了
------解决方案--------------------String srt = str.replaceall( "asdfasf ", "a ");
String[] srts = srt.split( " ");
System.out.println(srts.length-1);
------解决方案--------------------String c = "abcddgddeddf ";
int count = 0;
int last= 1;
while(last> 0)
{
count++;
last = c.indexOf( "d ",last+1);
}
System.out.println(count); //7;
------解决方案--------------------public static void main(String[] args) {
String c = "abcddgddeddfd ";
int count = 0;
for(int i=0;i <c.length();i++){
if(c.charAt(i) == 'd '){
count++;
}
}
System.out.println(count);
}