/**
* 计算一个字符串中包含某个子字符串的个数
* @author lin
* @param sourcestr 要检测的字符串
* @param findstr 要查找的子字符串
*/
public int substringCount(String sourcestr,String findstr)
{
int counter = 0;
int startposition = 0;
while(startposition < sourcestr.length()){
int search = sourcestr.indexOf(findstr, startposition);
if(search != -1){
counter++;
startposition = search + findstr.length();
}else{
break;
}
}
return counter;
}计算一个字符串中包含某个字符串的个数
最新推荐文章于 2023-12-06 12:44:47 发布
本文介绍了一种计算方法,用于确定一个给定字符串中特定子字符串出现的总次数。通过使用Java编程语言实现了一个实用的函数,该函数利用内置的indexOf方法来定位子字符串,并通过迭代确保所有实例都被正确计数。
3266

被折叠的 条评论
为什么被折叠?



