字符在字符串中出现的次数并输出

利用IndexOf && while

string text = "今天下雪了吗,明天不会下雪了吧,什么时候才不下雪啊,我要去上学啊!";
string keyWord = "下雪";
int index = 0;
int count = 0;
while ((index = text.IndexOf(keyWord,index)) != -1)
{
count++;
Console.WriteLine("第{0}次;索引是{1}", count, index);
index = index + keyWord.Length;
}
Console.WriteLine("下雪出现的总次数:{0}", count);

出自:https://www.cnblogs.com/xyxy/p/4369585.html

原文地址:https://www.cnblogs.com/mi21/p/10978242.html