Thursday 5 May 2011

Dot.Net: Get Letter String from ASCII index

Question:
How to get the string for a given ASCII value in CSharp ?
How to write a list of letters from A to Z in a loop in c# ?


Answer:
In C# you can get the string for a letter from its Unicode number by using the method ConvertFromUtf32 of the Char struct.

To write a list of all upper case letters you simply loop over the ascii values and get the string for each one.

Here is an example
 for (int i = 65; i <= 90; i++)
 {
     string s = Char.ConvertFromUtf32(i);
     Console.Write(s + " ");
 }

No comments: