c#.net中字母数字(字母数字++)值的自动递增

时间:2012-11-01 16:17:39

标签: c#-4.0

c#.net

中字母数字值的自动递增

输入文字 - >输出文本增量+1

56755 - > 56756

56759 - > 5675a

5675z - > 56761

zzz - > 1111

1 个答案:

答案 0 :(得分:0)

c#.net

中字母数字(字母数字++)值的自动增量
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlphaNumeric
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var programObj = new Program();

            Console.WriteLine(programObj.AlphaNumericIncrement("56755")); //   56756
            Console.WriteLine(programObj.AlphaNumericIncrement("56759")); //   5675a
            Console.WriteLine(programObj.AlphaNumericIncrement("5675z")); //   56761
            Console.WriteLine(programObj.AlphaNumericIncrement("zzz"));   //   1111

            Console.ReadLine();
        }

        public string AlphaNumericIncrement(string text)
        {
            if (text == null || text.Trim().Length == 0)
                return "1";
            else
            {
                text = text.Trim();
                string alphaNum = "123456789abcdefghijklmnopqrstuvwxyz";
                var collection = text.ToLower().Trim().ToCharArray().Reverse().ToList();
                bool isNextInr = true;
                int l = collection.Count() - 1, i = 0;
                while (isNextInr && i < collection.Count())
                {
                    isNextInr = false;
                    switch (collection[i])
                    {
                        case 'z':
                            collection[i] = '1';
                            if (i < l)
                                isNextInr = true;
                            else
                                collection.Add('1');
                            break;
                        default:
                            collection[i] = char.Parse(alphaNum.Substring(alphaNum.IndexOf(collection[i]) + 1, 1));
                            break;
                    }
                    i++;
                }
                collection.Reverse();
                return string.Join("", collection);
            }
        }
    }
}