54

我知道我可以使用该indexof()函数返回字符串的特定字符的索引,但是如何返回特定索引处的字符呢?

4

2 回答 2

81
string s = "hello";
char c = s[1];
// now c == 'e'

另请参见Substring, 返回多个字符。

于 2010-03-10T12:46:52.037 回答
12

你的意思是这样的

int index = 2;
string s = "hello";
Console.WriteLine(s[index]);

string 也实现IEnumberable<char>了,所以你也可以像这样枚举它

foreach (char c in s)
    Console.WriteLine(c);
于 2010-03-10T12:47:31.673 回答