2012-11-01 142 views

回答

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); 
      } 
     } 
    } 
} 
+0

字符串文本應該只包含字母和整數。沒有特殊字符。 – Thulasiram