programing

알파벳 문자 배열 생성

nasanasas 2020. 9. 17. 07:58
반응형

알파벳 문자 배열 생성


C #에서 알파벳 문자를 포함하는 배열을 생성하는 쉬운 방법이 있습니까? 손으로하는 것은 그리 어렵지 않지만 이렇게 할 수있는 방법이 있는지 궁금합니다.


내장 된 방식은 없다고 생각하지만 가장 쉬운 방법은

  char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

C # 3.0 :

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

예, Enumerable.Range의 유일한 오버로드가 int 매개 변수를 허용하더라도 작동합니다 ;-)


for (char letter = 'A'; letter <= 'Z'; letter++)
{
     Debug.WriteLine(letter);
}

char[] alphabet = Enumerable.Range('A', 26).Select(x => (char)x).ToArray();

1 기반 인덱스를 기반으로 MS 엑셀 열 코드 (A, B, C, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ...)를 얻기 위해 이것을 작성했습니다. (물론, 제로 기반으로 전환하는 것은 단순히 column--;시작 부분에서 벗어나는 것 입니다.)

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;
}

영어 알파벳의 글자를 의미한다고 가정하면 ...

    for ( int i = 0; i < 26; i++ )
    {
        Console.WriteLine( Convert.ToChar( i + 65 ) );
    }
    Console.WriteLine( "Press any key to continue." );
    Console.ReadKey();

문자의 ascii 값에 따라 다음과 같이 할 수 있습니다.

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 are just casting from the int value of the character to the character value - but, that only works for ascii characters not different languages etc.

EDIT: As suggested by Mehrdad in the comment to a similar solution, it's better to do this:

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

This casts the A character to it's int value and then increments based on this, so it's not hardcoded.


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);
}

Surprised no one has suggested a yield solution:

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

Example:

foreach (var c in Alphabet())
{
    Console.Write(c);
}

var alphabets = Enumerable.Range('A', 26).Select((num) => ((char)num).ToString()).ToList();

char alphaStart = Char.Parse("A");
char alphaEnd = Char.Parse("Z");
for(char i = alphaStart; i <= alphaEnd; i++) {
    string anchorLetter = i.ToString();
}

//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++;
    }

4 ways get English alphabet in Console:

public void ShowEnglishAlphabet()
{
    var firstLetter = 'a';
    var endLetter = 'z';
    for (var letter = firstLetter; letter <= endLetter; letter++)
    {
        Console.WriteLine($"{letter}-{letter.ToString().ToUpper()}");
    }
}

public void ShowEnglishAlphabetFromUnicodeTableDecNumber()
{
    var firstLetter = 97;
    var endLetter = 122;
    for (var letterNumberUnicodeTable = firstLetter; 
    letterNumberUnicodeTable <= endLetter; letterNumberUnicodeTable++)
    {
        Console.WriteLine($"{(char)letterNumberUnicodeTable}- 
        {((char)letterNumberUnicodeTable).ToString().ToUpper()}");
    }
}

public void ShowEnglishAlphabetUnicodeTableEscapeSequence()
{
    var firstLetter = '\u0061';
    var endLetter = '\u007A';
    for (var letterNumberUnicodeTable = firstLetter; 
    letterNumberUnicodeTable <= endLetter; letterNumberUnicodeTable++)
    {
        Console.WriteLine($"{letterNumberUnicodeTable}- 
        {letterNumberUnicodeTable.ToString().ToUpper()}");
    }
}   

public void ShowEnglishAlphabetUnicodeTableLinq()
{
    var alphabets = Enumerable.Range('a', 26).Select(letter => 
    ((char)letter).ToString()).ToList();
    foreach (var letter in alphabets)
    {
        Console.WriteLine($"{letter}-{letter.ToUpper()}");
    }
}

참고URL : https://stackoverflow.com/questions/314466/generating-an-array-of-letters-in-the-alphabet

반응형