0

c#.net中字母数值的自动增量

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

56755 --> 56756

56759 --> 5675a

5675z --> 56761

zzz --> 1111

4

1 回答 1

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);
            }
        }
    }
}
于 2012-11-01T16:17:39.263 回答