You can use the Substring
method to get any number of characters from a string at any given location. However, if you only want a single character, you can use the string indexer to get a single character at any given index like you do with an array:
string s = "hello";
char c = s[1]; //Returns 'e'
Notice that the return type is char
, unlike the Substring
method which returns a string
type.
You can also use the indexer to iterate through the characters of the string:
string s = "hello";
foreach (char c in s)
Console.WriteLine(c);
/********* This will print each character on a new line:
h
e
l
l
o
**********/