Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all latin (English alphabet) characters?

Tags:

string

c#

I am looking for an efficient way of getting a list with all English (latin) characters.

A, B, C, .... , Z

I really don't want a constructor like this:

// nasty way to create list of English alphabet
List<string> list = new List<string>();
list.Add("A");
list.Add("B");
....
list.Add("Z");

// use the list
...

If you are curius on how is this usable, I am creating a bin-tree mechanism.

like image 682
Odys Avatar asked Dec 10 '11 20:12

Odys


2 Answers

You can do this with a for loop:

List<char> list = new List<char>();
for (char c = 'A'; c <= 'Z'; ++c) {
    list.Add(c);
}

If you want a List<string> instead of a List<char> use list.Add(c.ToString()); instead.

Note that this works only because the letters A - Z occur in a consecutive sequence in Unicode (code points 65 to 90). The same approach does not necessarily work for other alphabets.

like image 164
Mark Byers Avatar answered Sep 30 '22 17:09

Mark Byers


Here:

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

This string is a list of characters.

Use either ToCharArray or the LINQ ToList to convert to an enumerable of your choice, though you can already access each item through the Chars indexer of the string.

like image 23
Oded Avatar answered Sep 30 '22 17:09

Oded