Odys
Odys

Reputation: 9090

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

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.

Upvotes: 6

Views: 10917

Answers (7)

Shawn Chen
Shawn Chen

Reputation: 11

Generate string and convert to array

string str = "abcdefghijklmnopqrstuvwxyz"; //create characters
char[] arr;  //create array
arr = str.ToCharArray(0, 26); //You can change the number to adjust this list

Get the value in the array

char getchar = arr[17];//get "r"

Upvotes: 0

Odys
Odys

Reputation: 9090

Here is my solution (eventually)

const string driveLetters = "DEFGHIJKLMNOPQRSTUVWXYZ";

List<string> allDrives = new List<string>(driveLetters.Length);
allDrives = (from letter
        in driveLetters.ToCharArray()
        select letter).ToList();

I ended up with this solution because initially my goal was to create a list of all available drives in windows. This is the actual code:

const string driveLetters = "DEFGHIJKLMNOPQRSTUVWXYZ";
const string driveNameTrails = @":\";

List<string> allDrives = (from letter
            in driveLetters.ToCharArray()
            select letter + driveNameTrails).ToList();

return allDrives;

Upvotes: 1

sll
sll

Reputation: 62544

Using LINQ

int charactersCount = 'Z' - 'A' + 1;
IList<char> all = Enumerable.Range('A', charactersCount)
                            .Union(Enumerable.Range('a', charactersCount))
                            .Select(i => (char)i)
                            .ToList();

Upvotes: 4

Oded
Oded

Reputation: 499142

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.

Upvotes: 12

Mark Byers
Mark Byers

Reputation: 838736

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.

Upvotes: 13

Maxim
Maxim

Reputation: 7348

The simplest way -

  int start = (int) 'A';
  int end = (int) 'Z';

  List<char> letters = new List<char>();

  for (int i = start; i <= end; i++)
  {
    letters.Add((char)i);  
  }

Same way but less code -

IEnumerable<char> linqLetters = Enumerable.Range(start, end - start + 1).Select(t => (char)t);

Upvotes: 0

rmc00
rmc00

Reputation: 887

There's no built in way to get a list of strings that correspond to each character. You can get an IEnumerable with the following code, which will probably suit your purposes. You could also just stick with the array in the from section.

var letters = from letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
              select letter.ToString();

Upvotes: 2

Related Questions