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();
posted on 2018-11-03 21:27  李易峰0528  阅读(135)  评论(0编辑  收藏  举报