Generating an array of letters in the alphabet

前端 未结 14 1584
粉色の甜心
粉色の甜心 2020-11-28 03:53

Is there an easy way to generate an array containing the letters of the alphabet in C#? It\'s not too hard to do it by hand, but I was wondering if there was a built in way

相关标签:
14条回答
  • 2020-11-28 04:05

    Unfortunately there is no ready-to-use way.

    You can use; char[] characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

    0 讨论(0)
  • 2020-11-28 04:09
    char alphaStart = Char.Parse("A");
    char alphaEnd = Char.Parse("Z");
    for(char i = alphaStart; i <= alphaEnd; i++) {
        string anchorLetter = i.ToString();
    }
    
    0 讨论(0)
  • 2020-11-28 04:11
    var alphabets = Enumerable.Range('A', 26).Select((num) => ((char)num).ToString()).ToList();
    
    0 讨论(0)
  • 2020-11-28 04:11
    //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 <= 25)
        {
            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 讨论(0)
  • 2020-11-28 04:13

    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 does work even if the only overload of Enumerable.Range accepts int parameters ;-)

    0 讨论(0)
  • 2020-11-28 04:17

    I don't think there is a built in way, but I think the easiest would be

      char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    
    0 讨论(0)
提交回复
热议问题