繁体   English   中英

如何通过索引获取字符串中的字符?

[英]How can I get a character in a string by index?

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

"

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

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

你的意思是这样吗

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

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

foreach (char c in s)
    Console.WriteLine(c);

 function asdf (){ alert('asdf'); }<\/code><\/pre>

"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM