递增使用数字和字符(也称为Base36数字)的索引 [英] Increment an index that uses numbers and characters (aka Base36 numbers)

查看:48
本文介绍了递增使用数字和字符(也称为Base36数字)的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于字符串的代码,其长度可以是两个或三个字符,我正在寻求一些帮助来创建一个将其递增的函数.

I have a string based code that can be either two or three characters in length and I am looking for some help in creating a function that will increment it.

代码的每个数字"的值为0到9,A到Z.

Each 'digit' of the code has a value of 0 to 9 and A to Z.

一些例子:

序列中的第一个代码是000

the first code in the sequence is 000

009-下一个代码是-00A
00D-下一个代码是-00E
AAZ-下一个代码是-AB0

009 - next code is - 00A
00D - next code is - 00E
AAZ - next code is - AB0

最后一个代码是ZZZ.

the last code is ZZZ.

希望这有道理.

推荐答案

感谢咨询专家.

这是我独立提出的.

    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';
    }

我不知道它是否更好/更糟,但似乎可以正常工作.如果有人可以提出改进建议,那就太好了.

I don't know if it is better/worse but seems to work. If anyone can suggest improvements then that is great.

这篇关于递增使用数字和字符(也称为Base36数字)的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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