我有一个基于字符串的代码,长度可以是两个或三个字符,我正在寻找一些帮助来创建一个可以增加它的函数。
代码的每个“数字”都有一个 0 到 9 和 A 到 Z 的值。
一些例子:
序列中的第一个代码是 000
009 - 下一个代码是 - 00A 
00D - 下一个代码是 - 00E 
AAZ - 下一个代码是 - AB0
最后一个代码是 ZZZ。
希望这有点道理。
将计数器保持为 int 并递增它。通过迭代地修改和除以 36 将 int 转换为您的字符表示。将修改后的范围 (0-35) 映射到 0-Z。
例子
更新了功能以朝任一方向前进:
internal class Program
{
    const int Base = 36;
    public static void Main()
    {
        Console.WriteLine(ToInt("0AA"));
        Console.WriteLine(ToString(370));
    }
    private static string ToString(int counter)
    {
        List<char> chars = new List<char>();
        do
        {
            int c = (counter % Base);
            char ascii = (char)(c + (c < 10 ? 48 : 55));
            chars.Add(ascii);
        }
        while ((counter /= Base) != 0);
        chars.Reverse();
        string charCounter = new string(chars.ToArray()).PadLeft(3, '0');
        return charCounter;
    }
    private static int ToInt(string charCounter)
    {
        var chars = charCounter.ToCharArray();
        int counter = 0;
        for (int i = (chars.Length - 1), j = 0; i >= 0; i--, j++)
        {
            int chr = chars[i];
            int value = (chr - (chr > 57 ? 55 : 48)) * (int)Math.Pow(Base, j);
            counter += value;
        }
        return counter;
    }
有关转换代码的更多变体,请参阅将基数 10 数字转换为 .NET 中任何基数的最快方法?.
谢谢各位的建议。
这是我独立提出的。
    private static String Increment(String s)
    {
        String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char lastChar = s[s.Length - 1];
        string fragment = s.Substring(0, s.Length - 1);
        if (chars.IndexOf(lastChar) < 35)
        {
            lastChar = chars[chars.IndexOf(lastChar) + 1];
            return fragment + lastChar;
        }
        return Increment(fragment) + '0';
    }
我不知道它是否更好/更差,但似乎有效。如果有人可以提出改进建议,那就太好了。
这能满足您的需要吗?
public class LetterCounter
{
    private static readonly string[] _charactersByIndex = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
    public string GetStr(int i)
    {
        if (i < _charactersByIndex.Length)
            return _charactersByIndex[i];
        int x = i / (_charactersByIndex.Length - 1) - 1;
        string a = _charactersByIndex[x];
        string b = GetStr(i - (_charactersByIndex.Length - 1));
        return a + b;
    }
}
}
根据@Martin 的回答,当两个 ZZ 出现时我发现了一些错误,这在代码中产生了异常
private static String Increment(String s,bool IsFromRecursion=false)
{
    String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //Added this condition
    if (IsFromRecursion && string.IsNullOrEmpty(number))
    {
         return "1";
    }
    //Added this condition
    char lastChar = s[s.Length - 1];
    string fragment = s.Substring(0, s.Length - 1);
    if (chars.IndexOf(lastChar) < 35)
    {
        lastChar = chars[chars.IndexOf(lastChar) + 1];
        return fragment + lastChar;
    }
    return Increment(fragment,true) + '0';
}
当我们调用这个方法时,我们只传递第一个参数。