in c#, how can i build up array from A to ZZ that is similar to the way that excel orders columns

后端 未结 10 1041
情深已故
情深已故 2020-12-18 00:48

i am looking for code that can generate an array where the first item is A, then B, then C . . .after Z i

10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 01:41

    Great answer of Vlad.

    Here's another variation on that:

     static IEnumerable 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 starting the sequence with an empty string you could write it as follows:

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

提交回复
热议问题