现在的位置: 首页 > 综合 > 正文

Visual C# 2008 实用开发详解–9.1.2 定位字符和子串

2012年02月21日 ⁄ 综合 ⁄ 共 422字 ⁄ 字号 评论关闭
   System.String使用从0开始的序号索引,来定位字符串中的单个字符。所以,可以利用访问数组的方式来定位字符。

示例代码演示了如何定位字符和子串。

static void Main(string[] args)

{

    string str = "这是一个定位字符串的示例";

    Console.WriteLine(str);

    Console.WriteLine("索引值为1的字符串为:"+str[1]);

    Console.WriteLine("索引值为4的字符串为:"+str[4]);

    Console.WriteLine("string类型实现了IEnumerable<char>IEnumerable接口,所以,可以用foreach遍历");

    foreach (char s in str)

    {               

        Console.Write(s);

    }

    Console.ReadLine();

}

抱歉!评论已关闭.