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();
}