C# 生成字母表中的字母数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/314466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating an array of letters in the alphabet
提问by Helephant
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# 中生成一个包含字母表的数组?手工制作并不太难,但我想知道是否有内置的方法来做到这一点。
采纳答案by Bob
I don't think there is a built in way, but I think the easiest would be
我不认为有内置的方式,但我认为最简单的方法是
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
回答by xan
You could do something like this, based on the ascii values of the characters:
您可以根据字符的 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.
(请参阅此处的表格。)您只是将字符的 int 值转换为字符值 - 但是,这只适用于 ascii 字符,而不适用于不同的语言等。
EDIT:As suggested by Mehrdadin the comment to a similar solution, it's better to do this:
编辑:正如Mehrdad在对类似解决方案的评论中所建议的那样,最好这样做:
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.
这将 A 字符转换为它的 int 值,然后基于此递增,因此它不是硬编码的。
回答by rp.
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();
回答by Pop Catalin
C# 3.0 :
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 ;-)
是的,即使 Enumerable.Range 的唯一重载接受 int 参数,它也确实有效;-)
回答by James Curran
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:
另请注意,该字符串有一个 operator[],它返回一个 Char,并且是一个IEnumerable<char>
,因此在大多数情况下,您可以将字符串用作 char[]。因此:
string alpha = "ABCDEFGHIJKLMNOPQRSTUVQXYZ";
for (int i =0; i < 26; ++i)
{
Console.WriteLine(alpha[i]);
}
foreach(char c in alpha)
{
Console.WriteLine(c);
}
回答by aa.
char alphaStart = Char.Parse("A");
char alphaEnd = Char.Parse("Z");
for(char i = alphaStart; i <= alphaEnd; i++) {
string anchorLetter = i.ToString();
}
回答by rjdmello
//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++;
}
回答by Nyerguds
I wrote this to get the MS excel column code (A,B,C, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ...) based on a 1-based index. (Of course, switching to zero-based is simply leaving off the column--;
at the start.)
我写这个是为了获得基于 1 的索引的 MS excel 列代码(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;
}
回答by Simon
for (char letter = 'A'; letter <= 'Z'; letter++)
{
Debug.WriteLine(letter);
}
回答by weston
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);
}