Mega Code Archive

 
Categories / C# Book / 02 Essential Types
 

0233 Access the character in a string

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