To access the characters in a string by index we use the indexer of string type.
using System;
class Sample
{
public static void Main()
{
string s = "rntsoft.com";
Console.WriteLine(s[2]);
}
}
The output:
v
string type implements the IEnumerable interface, therefore it is handy to access each character in a string with foreach loop.
using System;
class Sample
{
public static void Main()
{
string s = "rntsoft.com";
foreach (char ch in s)
{
Console.WriteLine(ch);
}
}
}