indexof
string msg = "liyifeng"; //判断这个字符串中出现i的次数,和每次出现的索引位置 int count = 0; string keywords = "i"; int index = 0; while ((index = msg.IndexOf(keywords, index)) != -1) { count++; Console.WriteLine("第{0}次出现【{1}】,索引是:{2}", count, keywords, index); //下次从这个词末的位置开始查找,所以要跳过本次该词的长度个字符位置继续查找 index = index + keywords.Length; } Console.WriteLine("=========================================="); Console.WriteLine("【{0}】一次一共出现了{1}次。", keywords, count);
Console.ReadLine();