得到str1在str2中出现的次数 GetCountInStr(string str1,string str2)
2006-03-09 10:22
267 查看
/// <summary>
/// 得到str1在str2中出现的次数
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static int GetCountInStr(string str1,string str2){
int count = 0;
int str1Len = str1.Length;
for(int i=0;i<=str2.Length-str1Len;i++){
if(str2.Substring(i,str1Len) == str1)
count ++;
}
return count;
}
/// 得到str1在str2中出现的次数
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static int GetCountInStr(string str1,string str2){
int count = 0;
int str1Len = str1.Length;
for(int i=0;i<=str2.Length-str1Len;i++){
if(str2.Substring(i,str1Len) == str1)
count ++;
}
return count;
}
相关文章推荐
- Python3基础 str count 得到一个字符串出现的次数
- ASP计算str2在str1中出现的次数
- ASP计算str2在str1中出现的次数
- strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
- 在排序数组中的出现次数 Count the number of occurrences in a sorted array
- Python:string.count()返回str在string里面出现的次数
- 如果用JS得到字符串中出现次数最多的字母
- 在Matlab中,想要将两个字符串连接在一起,有以下的方法: 假定有两个字符串 >> str1='Iloveyou';str2='123'; 方法一:用中括号将str1和str2像矩阵元素一样包含起来
- leetcode 260. Single Number III --> 用hashmap计算出现的次数,注意containsKey获取key,用 get 方法获取值
- 字符串替换 eg: str1="123%s456%s" str2={'a','b','c','d'}(将str1中的%s替换成str2中的字符)替换结果返回为:123a456bcd
- php数组函数序列 之array_count_values() 统计数组中所有值出现的次数函数
- Spark primer 之wordcount 剖析(添加 spark按照单词出现次数排名)
- PHP中substr_count()函数获取子字符串出现次数的方法
- 使用CCrossbar类调用GetInputCount得到错误输入端个数的解决方案
- How Would You Get the Count of an Array in C++?
- How Would You Get the Count of an Array in C++?
- 编写函数void count(char a[],char w[][10],int n,int b[])。功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字母字符看作单词分
- 用array_count_values统计一篇英文文档中每个单词的出现次数,结果用表格展示出来
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)