《零基础学C#》第六章-实例01:查找“r”在“We are the world”中出现的位置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wtxhai/article/details/88525599

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example601
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "We are the world";          //创建字符串
            int firstIndex = str.IndexOf("r");      //获取字符串中“r”第一次出现的位置
            int secondIndex = str.IndexOf("r", firstIndex + 1);  //获取字符串中“r”第二次出现的位置
            int thirdIndex = str.IndexOf("r", secondIndex + 1 );  //获取字符串中“r”第三次出现的位置
            //输出三次出现的位置
            Console.WriteLine("r第一次出现的位置:" + firstIndex);
            Console.WriteLine("r第二次出现的位置:" + secondIndex);
            Console.WriteLine("r第三次出现的位置:" + thirdIndex);
            Console.ReadLine();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wtxhai/article/details/88525599