0
自動增量在C#.NET字母數字值自動增量字母數字(字母數字++)值在C#.net
輸入文本 - >輸出文本以增量1
56755 - - > 56756
56759 - > 5675a
5675z - > 56761
ZZZ - > 1111
自動增量在C#.NET字母數字值自動增量字母數字(字母數字++)值在C#.net
輸入文本 - >輸出文本以增量1
56755 - - > 56756
56759 - > 5675a
5675z - > 56761
ZZZ - > 1111
自動增量字母數字(字母數字++)值在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);
}
}
}
}
字符串文本應該只包含字母和整數。沒有特殊字符。 – Thulasiram
@喬恩B,bažmegakapa,肖恩·歐文,傑森 - 海涅和博佩爾森:u能提供我回答這個問題? – Thulasiram