例如有如下字符串
string a=123dsfsadfsa123adsfads
string b=123
我如何知道b在a中出现的次数
string a="123dsfsadfsa123adsfads";
string b="123";
string tempstr = a;
tempstr=tempstr.Replace(b, "");
int charcount=(a.Length-tempstr.Length);
int count=0;
if (charcount > 0)
count = charcount / b.Length;
本文介绍了一种简单的方法来计算一个子串在另一个字符串中出现的次数。通过使用C#中的Replace方法替换掉目标子串,并对比替换前后的长度差来得出出现次数。
798

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



