在 C# 中生成字母序列的逻辑 [英] Logic to generate an alphabetical sequence in C#

查看:107
本文介绍了在 C# 中生成字母序列的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

顺序应该是这样的.
A-Z、AA-AZ、BA-BZ、CA-CZ、......、ZA-ZZ
ZZ之后应该从AAA开始.
然后AAAZZZ,然后AAAAZZZZ等等.

The sequence should go like this.
A-Z,AA-AZ,BA-BZ,CA-CZ,.......,ZA-ZZ
After ZZ it should start from AAA.
Then AAA to ZZZ and then AAAA to ZZZZ and so on.

此序列与 Excel 工作表的序列非常相似.

This sequence is pretty much like that of an Excel sheet.

添加了我的代码

        private void SequenceGenerator()
    {
        var numAlpha = new Regex("(?<Numeric>[0-9]*)(?<Alpha>[a-zA-Z]*)");
        var match = numAlpha.Match(txtBNo.Text);

        var alpha = match.Groups["Alpha"].Value;
        var num = Convert.ToInt32(match.Groups["Numeric"].Value);
        lastChar = alpha.Substring(alpha.Length - 1);

        if (lastChar=="Z")
        {
            lastChar = "A";
            txtBNo.Text = num.ToString() + "A" + alpha.Substring(0, alpha.Length - 1) + lastChar;
        }

        else
        {
            txtBNo.Text = num.ToString() + alpha.Substring(0, alpha.Length - 1) + Convert.ToChar(Convert.ToInt32(Convert.ToChar(lastChar)) + 1);
        }
    }

这就是我所做的.但是,我知道这是一个错误的逻辑.

This is what I've done. But, I know that is a wrong logic.

谢谢.

推荐答案

很幸运,我以前做过一次.我遇到的问题是,在 Excel 工作表中没有 0,甚至在两位数字"数字"中也没有.这意味着您从 a(即 1)开始,然后从 z(即 26)直接到 aa(27).这就是为什么不是简单的基转换问题,您需要一些额外的代码来处理这个问题.使用以下结果测试 xanatos 建议的函数:

Lucky for you, I've done this once before. the problems I've encountered is that in the Excel sheet there is no 0, not even in double 'digit' 'numbers'. meaning you start with a (that's 1) and then from z (that's 26) you go straight to aa (27). This is why is't not a simple base conversion problem, and you need some extra code to handle this. Testing the function suggested by xanatos results with the following:

NumToLetters(0) --> A

NumToLetters(0) --> A

NumToLetters(25) --> Z

NumToLetters(25) --> Z

NumToLetters(26) --> BA

NumToLetters(26) --> BA

我的解决方案有更多代码,但已经针对 Excel 进行了测试并且完全兼容,只是它以 0 开头而不是 1,这意味着 a 是 0,z 是 25,aa 是 26,zz 701,aaa 是 702 和很快).如果需要,您可以将其更改为从 1 开始,这很容易.

My solution has more code but it has been tested against Excel and is fully compatible, except it starts with 0 and not 1, meaning that a is 0, z is 25, aa is 26, zz 701, aaa is 702 and so on). you can change it to start from 1 if you want, it's fairly easy.

private static string mColumnLetters = "zabcdefghijklmnopqrstuvwxyz";

// Convert Column name to 0 based index
public static int ColumnIndexByName(string ColumnName)
    {
        string CurrentLetter;
        int ColumnIndex, LetterValue, ColumnNameLength;
        ColumnIndex = -1; // A is the first column, but for calculation it's number is 1 and not 0. however, Index is alsways zero-based.
        ColumnNameLength = ColumnName.Length;
        for (int i = 0; i < ColumnNameLength; i++)
        {
            CurrentLetter = ColumnName.Substring(i, 1).ToLower();
            LetterValue = mColumnLetters.IndexOf(CurrentLetter);
            ColumnIndex += LetterValue * (int)Math.Pow(26, (ColumnNameLength - (i + 1)));
        }
        return ColumnIndex;
    }

// Convert 0 based index to Column name
public static string ColumnNameByIndex(int ColumnIndex)
    {
        int ModOf26, Subtract;
        StringBuilder NumberInLetters = new StringBuilder();
        ColumnIndex += 1; // A is the first column, but for calculation it's number is 1 and not 0. however, Index is alsways zero-based.
        while (ColumnIndex > 0)
        {
            if (ColumnIndex <= 26)
            {
                ModOf26 = ColumnIndex;
                NumberInLetters.Insert(0, mColumnLetters.Substring(ModOf26, 1));
                ColumnIndex = 0;
            }
            else
            {
                ModOf26 = ColumnIndex % 26;
                Subtract = (ModOf26 == 0) ? 26 : ModOf26;
                ColumnIndex = (ColumnIndex - Subtract) / 26;
                NumberInLetters.Insert(0, mColumnLetters.Substring(ModOf26, 1));
            }
        }
        return NumberInLetters.ToString().ToUpper();
    }

这篇关于在 C# 中生成字母序列的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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