遍历字母表 - C# a-caz [英] Iterating through the Alphabet - C# a-caz

查看:38
本文介绍了遍历字母表 - C# a-caz的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于遍历 Alphabet 的问题.我想要一个以a"开头并以z"结尾的循环.之后,循环开始aa"并计数到az".之后从ba"开始到bz"等等......

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...

有人知道解决办法吗?

我忘了我给函数一个字符a"然后函数必须返回b.如果你给出bnc",那么函数必须返回bnd"

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"

推荐答案

完全按照 OP 最新编辑的要求进行

这是最简单的解决方案,经过测试:

This is the simplest solution, and tested:

static void Main(string[] args)
{
    Console.WriteLine(GetNextBase26("a"));
    Console.WriteLine(GetNextBase26("bnc"));
}

private static string GetNextBase26(string a)
{
    return Base26Sequence().SkipWhile(x => x != a).Skip(1).First();
}

private static IEnumerable<string> Base26Sequence()
{
    long i = 0L;
    while (true)
        yield return Base26Encode(i++);
}

private static char[] base26Chars = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
private static string Base26Encode(Int64 value)
{
    string returnValue = null;
    do
    {
        returnValue = base26Chars[value % 26] + returnValue;
        value /= 26;
    } while (value-- != 0);
    return returnValue;
}

这篇关于遍历字母表 - C# a-caz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆