JAVA判断字符串中某个字符存在的个数

时间:2023-03-08 22:21:01
/**
* 判断字符串中某个字符存在的个数
* @param str1 完整字符串
* @param str2 要统计匹配个数的字符
* @return
*/
public static int countStr(String str1, String str2) {
int count=0;
if (str1.indexOf(str2) == -1) {
return 0;
}
while(str1.indexOf(str2)!=-1){
count++;
str1=str1.substring(str1.indexOf(str2)+str2.length());
}
return count;
}