Increment an index that uses numbers and characters (aka Base36 numbers)

后端 未结 4 752
感动是毒
感动是毒 2021-01-19 19:08

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.

Each \'

4条回答
  •  梦毁少年i
    2021-01-19 19:49

    Based on @Martin answer, I found some error when two ZZ comes, this makes exception in code

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

    When we call this method we pass first parameter only.

提交回复
热议问题