CSharp - String characters Indexer Accessing

Introduction

A string's indexer returns a single character at the given index.

The indexing is is zero-indexed:

Demo

using System;
class MainClass{//from  w w w  . j  ava  2 s.co m
   public static void Main(string[] args){
         string str  = "abcde";
         char letter = str[1];        // letter == 'b'
   }
}

string implements IEnumerable<char>, so you can foreach over its characters:

Demo

using System;
class MainClass{/*ww  w .  j  a  va 2s  . c o  m*/
   public static void Main(string[] args){
     foreach (char c in "123")
        Console.Write (c + ",");    // 1,2,3,
   }
}

Result

Exercise