C#A-CAZ - 通过迭代字母 [英] Iterating through the Alphabet - C# a-caz

查看:244
本文介绍了C#A-CAZ - 通过迭代字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有关于迭代throught字母表问题。
我想有一个循环,以A开头,以Z结尾。在这以后,循环开始AA数到AZ。后以BA开头到BZ等等...

Hi i have question about iterate throught 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?

编辑:我忘了,我举个字符A的功能,那么该函数必须返回b。如果你给BNC,则函数必须返回BND

I forgot that i give an 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天全站免登陆