Generating an array of letters in the alphabet

2019-01-07 05:01发布

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 to do this.

标签: c# alphabet
12条回答
姐就是有狂的资本
2楼-- · 2019-01-07 05:42

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

string alpha = "ABCDEFGHIJKLMNOPQRSTUVQXYZ";
for (int i =0; i < 26; ++i)
{  
     Console.WriteLine(alpha[i]);
}

foreach(char c in alpha)
{  
     Console.WriteLine(c);
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-07 05:45
for (char letter = 'A'; letter <= 'Z'; letter++)
{
     Debug.WriteLine(letter);
}
查看更多
【Aperson】
4楼-- · 2019-01-07 05:46
char alphaStart = Char.Parse("A");
char alphaEnd = Char.Parse("Z");
for(char i = alphaStart; i <= alphaEnd; i++) {
    string anchorLetter = i.ToString();
}
查看更多
Bombasti
5楼-- · 2019-01-07 05:54
var alphabets = Enumerable.Range('A', 26).Select((num) => ((char)num).ToString()).ToList();
查看更多
Fickle 薄情
6楼-- · 2019-01-07 05:54
//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++;
    }
查看更多
我只想做你的唯一
7楼-- · 2019-01-07 05:58

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();
查看更多
登录 后发表回答