In C #, how can I create an array from A to ZZ that looks like the way order columns are superior

I am looking for code that can generate an array, where the first element is A , then B , then C.

What is the best way to do this in C #?

+7
source share
9 answers

One of the methods:

IEnumerable<string> generate() { for (char c = 'A'; c <= 'Z'; c++) yield return new string(c, 1); for (char c = 'A'; c <= 'Z'; c++) for (char d = 'A'; d <= 'Z'; d++) yield return new string(new[] { c, d }); } 

Edit:
you can actually create an infinite sequence (limited by the maximum long value) with slightly more complex code:

 string toBase26(long i) { if (i == 0) return ""; i--; return toBase26(i / 26) + (char)('A' + i % 26); } IEnumerable<string> generate() { long n = 0; while (true) yield return toBase26(++n); } 

This happens as follows: A, B, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ... etc .:

 foreach (var s in generate().Take(200)) Console.WriteLine(s); 
+21
source

Great answer Vlad.

Here is another option:

  static IEnumerable<string> generate() { for (char c = 'A'; c <= 'Z'; c++) { yield return c.ToString(); } foreach (string s in generate()) { for (char c = 'A'; c <= 'Z'; c++) { yield return s + c; } } } 

If you don't mind running a sequence with an empty string, you can write it like this:

  static IEnumerable<string> generate() { yield return ""; foreach (string s in generate()) { for (char c = 'A'; c <= 'Z'; c++) { yield return s + c; } } } 
+3
source

You can generate numbers with Enumerable.Range and pass them to char to generate AZ. The next step is to combine them.

+1
source

This is found in SO, albeit in PHP. Will this help? Algorithm to get excel column name of type

+1
source
 var q = Enumerable.Range(Convert.ToInt32('A'),26).Select( x => Convert.ToChar(x) ); var result = ( q.Select( x => x.ToString() ) .Concat( q.SelectMany( x => q.Select( y => x.ToString() + y.ToString() ) ) ) ); 
+1
source
 class Program { public static string IntegerToExcelColumn(int col) { // I've put a 256 upper bound here because Excel 2003 // allows only 256 columns. Change it if you're using // Excel 2007 or 2010. Debug.Assert(col >= 1 && col <= 256); if (col >= 1 && col <= 26) { return ((char)(((int)'A') + (col - 1))).ToString(); } // I've put a 256 upper bound here because Excel 2003 // allows only 256 columns. Change it if you're using // Excel 2007 or 2010. if (col > 26 && col <= 256) { int rem = col % 26; int pri = col / 26; if (rem == 0) { rem = 26; pri--; } char[] buffer = new char[2]; buffer[0] = (char)(((int)'A') + (pri - 1)); buffer[1] = (char)(((int)'A') + (rem - 1)); return new string(buffer); } return ""; } static void Main(string[] args) { string[] columns= new string[255]; for (int i = 1; i <= 255; i++) columns[i-1] = IntegerToExcelColumn(i); foreach(var col in columns) Console.WriteLine(col); } } 
+1
source

Here is one way. :)

 string[] values = Enumerable.Range(0, 27 * 26) .Select( n => new String( new[] { (char)('@' + n / 26), (char)('A' + n % 26) }, n < 26 ? 1 : 0, n < 26 ? 1 : 2 ) ) .ToArray(); 
+1
source

Look at the ASCII table and pay attention to the meaning of the characters. You should be able to cycle to increase a character from A to Z as many times as you need to replicate characters. :)

0
source

You can also try this. A to ZZ

 public class Program { public static void Main() { int c=1; string cc=AtoZZ(c); for(int i=1;i<=702;i++){ cc=AtoZZ(i); Console.WriteLine(cc); } } static string AtoZZ(int chr) { char c1; char c2; string alp = ""; if(chr<=26) { c1 = (char)(chr + 64); alp = c1 + ""; } else { int cl1 = chr / 26; int cl2 = chr % 26; if(cl2==0){ cl1--; cl2=26; } c1 = (char)(cl1 + 64); c2 = (char)(cl2 + 64); alp = c1 + "" + c2; } return alp; } } 
0
source

All Articles