迭代字母表 - C# a-caz
我有一个关于迭代字母表的问题。 我想要一个以“a”开头并以“z”结尾的循环。 之后,循环开始“aa”并计数到“az”。 之后从“ba”开始到“bz”等等......
有人知道一些解决方案吗?
谢谢
编辑:我忘记了我给了函数一个字符“a”,然后函数必须返回b。 如果你给出“bnc”,那么该函数必须返回“bnd”
I have a question about iterate through the Alphabet.
I would like to have a loop that begins with "a" and ends with "z". After that, the loop begins "aa" and count to "az". after that begins with "ba" up to "bz" and so on...
Anybody know some solution?
Thanks
EDIT: I forgot that I give a char "a" to the function then the function must return b. if u give "bnc" then the function must return "bnd"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
第一次尝试,先使用 az,然后使用 aa-zz
请注意,这将在“zz”处停止。 当然,就循环而言,这里存在一些难看的重复。 幸运的是,这很容易修复 - 而且它也可以更加灵活:
第二次尝试:更灵活的字母表
现在,如果您只想生成 a、b、c、d、aa、ab、ac , ad, ba, ...您可以调用
GetExcelColumns("abcd")
。第三次尝试(进一步修改) - 无限序列
使用递归可能会是更清晰的代码,但效率不会那么高。
请注意,如果您想在某个点停止,您可以使用 LINQ:
“重新启动”迭代器
要从给定点重新启动迭代器,您确实可以使用
SkipWhile
按照 thesoftwarejedi 的建议。 当然,这是相当低效的。 如果您能够在调用之间保留任何状态,则可以只保留迭代器(对于任一解决方案):或者,您也可以将代码构造为使用
foreach
,只需打破找出您可以实际使用的第一个值。First effort, with just a-z then aa-zz
Note that this will stop at 'zz'. Of course, there's some ugly duplication here in terms of the loops. Fortunately, that's easy to fix - and it can be even more flexible, too:
Second attempt: more flexible alphabet
Now if you want to generate just a, b, c, d, aa, ab, ac, ad, ba, ... you'd call
GetExcelColumns("abcd")
.Third attempt (revised further) - infinite sequence
It's possible that it would be cleaner code using recursion, but it wouldn't be as efficient.
Note that if you want to stop at a certain point, you can just use LINQ:
"Restarting" the iterator
To restart the iterator from a given point, you could indeed use
SkipWhile
as suggested by thesoftwarejedi. That's fairly inefficient, of course. If you're able to keep any state between call, you can just keep the iterator (for either solution):Alternatively, you may well be able to structure your code to use a
foreach
anyway, just breaking out on the first value you can actually use.编辑:使其完全按照OP最新编辑的要求
这是最简单的解决方案,并经过测试:
Edit: Made it do exactly as the OP's latest edit wants
This is the simplest solution, and tested:
我知道这里有很多答案,其中一个已被接受,但在我看来,它们都让事情变得比需要的更困难。 我认为以下内容更简单、更清晰:
请注意,这不会进行任何输入验证。 如果您不信任调用者,则应在开头添加
IsNullOrEmpty
检查,并添加c[i] >= 'A' && c[i] <= 'Z' || c[i] >= 'a' && c[i] <= 'z'
在循环顶部检查。 或者就不管它,让它成为 GIGO。您还可能会发现这些伴随函数的用途:
这两个函数是从零开始的。 即,“A”= 0、“Z”= 25、“AA”= 26 等。要使它们基于 1(如 Excel 的 COM 接口),请删除每个函数中注释行上方的行,并取消注释这些函数线。
与
NextColumn
函数一样,这些函数不会验证其输入。 如果这就是他们得到的,他们都会给你垃圾。I know there are plenty of answers here, and one's been accepted, but IMO they all make it harder than it needs to be. I think the following is simpler and cleaner:
Note that this doesn't do any input validation. If you don't trust your callers, you should add an
IsNullOrEmpty
check at the beginning, and ac[i] >= 'A' && c[i] <= 'Z' || c[i] >= 'a' && c[i] <= 'z'
check at the top of the loop. Or just leave it be and let it be GIGO.You may also find use for these companion functions:
These two functions are zero-based. That is, "A" = 0, "Z" = 25, "AA" = 26, etc. To make them one-based (like Excel's COM interface), remove the line above the commented line in each function, and uncomment those lines.
As with the
NextColumn
function, these functions don't validate their inputs. Both with give you garbage if that's what they get.以下内容使用所需的字符串填充列表:
The following populates a list with the required strings:
这就是我的想法。
Here’s what I came up with.
只是好奇,为什么不只是
just curious , why not just
这就像显示一个 int,仅使用基数 26 而不是基数 10。尝试以下算法来查找数组的第 n 个条目
当然,如果您想要前 n 个条目,这不是最有效的解决方案。 在这种情况下,请尝试类似丹尼尔的解决方案。
This is like displaying an int, only using base 26 in stead of base 10. Try the following algorithm to find the nth entry of the array
Of course, if you want the first n entries, this is not the most efficient solution. In this case, try something like daniel's solution.
我尝试了一下并想出了这个:
I gave this a go and came up with this:
这是我准备的一些可能类似的东西。 我正在尝试迭代计数,以便设计一个尽可能小的编号模式,但又给我足够的唯一性。
我知道每次添加一个 Alpha 字符,可能性都会增加 26 倍,但我不确定我想使用多少个字母、数字或模式。
这引导我到下面的代码。 基本上,你向它传递一个 AlphaNumber 字符串,每个有字母的位置最终都会增加到“z\Z”,每个有数字的位置最终会增加到“9”。
所以你可以称之为两种方式之一..
(对我来说,我正在做类似“1AA000”的事情)
Here is something I had cooked up that may be similar. I was experimenting with iteration counts in order to design a numbering schema that was as small as possible, yet gave me enough uniqueness.
I knew that each time a added an Alpha character, it would increase the possibilities 26x but I wasn't sure how many letters, numbers, or the pattern I wanted to use.
That lead me to the code below. Basically you pass it an AlphaNumber string, and every position that has a Letter, would eventually increment to "z\Z" and every position that had a Number, would eventually increment to "9".
So you can call it 1 of two ways..
(For me, I was doing something like "1AA000")
这是我使用递归的尝试:
然后只需调用
PrintAlphabet("abcd", "")
;Here's my attempt using recursion:
Then simply call
PrintAlphabet("abcd", "")
;