public int getCount(String mainStr,String subStr){
int minLength=mainStr.length();
int subLength=subStr.length();
int count=0;
int index=0;
if(minLength>=subLength){
/* 方式一
while ((index=mainStr.indexOf(subStr))!=-1) {
count++;
mainStr=mainStr.substring(index+subStr.length());
}
*/
while ((index=mainStr.indexOf(subStr,index))!=-1){
count++;
index+=subLength;
}
return count;
}
return -1;
}
java获取一个字符串,从中查找指定字符串出现的个数。
最新推荐文章于 2025-03-04 08:56:06 发布
本文介绍如何使用Java编程语言来统计一个字符串中特定子字符串出现的次数。通过示例代码详细解析字符串搜索和计数的实现过程。
8881





