Creating an array of letters in the alphabet

Is there an easy way to generate an array containing the letters of the alphabet in C #? It is not too difficult to do it manually, but I was wondering if there is a built-in way to do this.

+84
c #
Nov 24 '08 at 15:25
source share
11 answers

I donโ€™t think there is a built-in way, but I think that the easiest will be

char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); 
+182
Nov 24 '08 at 15:29
source share

C # 3.0:

 char[] az = Enumerable.Range('a', 'z' - 'a' + 1).Select(i => (Char)i).ToArray(); foreach (var c in az) { Console.WriteLine(c); } 

yes, it works even if a single overload of Enumerable.Range accepts int parameters; -)

+98
Nov 24 '08 at 15:39
source share
 for (char letter = 'A'; letter <= 'Z'; letter++) { Debug.WriteLine(letter); } 
+50
Aug 11 '11 at 4:50
source share
 char[] alphabet = Enumerable.Range('A', 26).Select(x => (char)x).ToArray(); 
+30
Feb 14 '14 at 2:56
source share

I wrote this to get Excel excel column code (A, B, C, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ...) based on a 1-based index. (Of course, going to zero is based on the simplicity of column--; at the beginning.)

 public static String getColumnNameFromIndex(int column) { column--; String col = Convert.ToString((char)('A' + (column % 26))); while (column >= 26) { column = (column / 26) -1; col = Convert.ToString((char)('A' + (column % 26))) + col; } return col; } 
+20
Mar 11 2018-11-11T00:
source share

Assuming you mean the letters of the English alphabet ...

  for ( int i = 0; i < 26; i++ ) { Console.WriteLine( Convert.ToChar( i + 65 ) ); } Console.WriteLine( "Press any key to continue." ); Console.ReadKey(); 
+13
Nov 24 '08 at 15:36
source share

You can do something similar based on ascii character values:

 char[26] alphabet; for(int i = 0; i <26; i++) { alphabet[i] = (char)(i+65); //65 is the offset for capital A in the ascaii table } 

(see the table here .) You simply throw from the int value of the character into the character value - but this only works for ascii characters of different languages, etc.

EDIT: As suggested by Mehrdad in a comment on a similar solution, it is better to do this:

 alphabet[i] = (char)(i+(int)('A')); 

This causes the character A to assign an int value to it, and then increment it based on this, so it is not hard-coded.

+6
Nov 24 '08 at 15:35
source share

Note that the string has a [] operator that returns Char, and is an IEnumerable<char> , so for most purposes you can use the string as char []. Hence:

 string alpha = "ABCDEFGHIJKLMNOPQRSTUVQXYZ"; for (int i =0; i < 26; ++i) { Console.WriteLine(alpha[i]); } foreach(char c in alpha) { Console.WriteLine(c); } 
+5
Nov 24 '08 at 16:49
source share

Surprised, no one offered a solution to exit:

 public static IEnumerable<char> Alphabet() { for (char letter = 'A'; letter <= 'Z'; letter++) { yield return letter; } } 

Example:

 foreach (var c in Alphabet()) { Console.Write(c); } 
+3
Dec 18 '12 at 11:15
source share
 char alphaStart = Char.Parse("A"); char alphaEnd = Char.Parse("Z"); for(char i = alphaStart; i <= alphaEnd; i++) { string anchorLetter = i.ToString(); } 
0
Oct 19 '09 at 22:33
source share
 //generate a list of alphabet using csharp //this recurcive function will return you //a string with position of passed int //say if pass 0 will return A ,1-B,2-C,.....,26-AA,27-AB,....,701-ZZ,702-AAA,703-AAB,... static string CharacterIncrement(int colCount) { int TempCount = 0; string returnCharCount = string.Empty; if (colCount <> { TempCount = colCount; char CharCount = Convert.ToChar((Convert.ToInt32('A') + TempCount)); returnCharCount += CharCount; return returnCharCount; } else { var rev = 0; while (colCount >= 26) { colCount = colCount - 26; rev++; } returnCharCount += CharacterIncrement(rev-1); returnCharCount += CharacterIncrement(colCount); return returnCharCount; } } //--------this loop call this function---------// int i = 0; while (i <> { string CharCount = string.Empty; CharCount = CharacterIncrement(i); i++; } 
0
Jul 16 '10 at 11:36
source share



All Articles