我希望以这种格式生成序列号
00000A
00000B
00000B
依此类推
00000Z
然后
00001A
00001B
00001C
...
00001Z
...
00010A
,直到
99999Z
我知道我可以使用这种方法生成260万行,但我想这就足够了
所以,如果我有一个字符串,让我们说26522C,现在我希望下一个数字为26522D 或者如果我有34287Z,我想要34288A
我可以编写关于它的算法但是会有很多字符
解析输入字符串字符我想知道有没有更简单的方法呢
String GetNextNumberInSequence(String inputString)
{
if (inputString.Length == 6)
{
var charArray = inputString.ToCharArray();
char[] inputChars = { charArray[0], charArray[1], charArray[2],charArray[3],charArray[4],charArray[5] };
if(Char.IsDigit(charArray[5]))
{
//Parse first 5 characters
}
}
}
答案 0 :(得分:1)
您可以通过将数字转换为 Base36 来实现此目的。
看一下这个样本:
private const string CharList = "0123456789abcdefghijklmnopqrstuvwxyz";
public static String Base36Encode(long input, char paddingChar, int totalWidth)
{
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 36]);
input /= 36;
}
return new string(result.ToArray()).PadLeft(totalWidth, paddingChar).ToUpper();
}
然后以这种方式使用它:
for(int i = 0; i < 1000; i++)
{
Debug.WriteLine(Base36Encode(i, '0', 6));
}
会产生这个:
000000,000001,000002,000003,000004,000005,000006,000007,000008,000009,00000A,00000B,00000C,00000D,00000E,00000F,00000G,00000H,00000I,00000J,00000K,00000L,00000M,00000N ,00000O,00000P,00000Q,00000R,00000S,00000T,00000U,00000V,00000W,00000X,00000Y,00000Z,000010,000011,000012,000013,000014,000015,000016,000017,000018,000019,00001A,00001B,00001C ,00001D,00001E,00001F,00001G,00001H,00001I,00001J,00001K,00001L,00001M,00001N,00001O,00001P,00001Q,00001R,00001S,00001T,00001U,00001V,00001W,00001X,00001Y,00001Z,000020,000021 ,000022,000023,000024,000025,000026,000027,000028,000029,00002A,00002B,00002C,00002D,00002E,00002F,00002G,00002H,00002I,00002J,00002K,00002L,00002M,00002N,00002O,00002P,00002Q ,00002R,00002S,00002T ......
这种方法的积极之处在于您可以使用以下方法将其转换回数字:
public static Int64 Base36Decode(string input)
{
var reversed = input.ToLower().Reverse();
long result = 0;
int pos = 0;
foreach (char c in reversed)
{
result += CharList.IndexOf(c) * (long)Math.Pow(36, pos);
pos++;
}
return result;
}
答案 1 :(得分:1)
private static String GetNextNumberInSequence(String inputString)
{
var integerpart = int.Parse(inputString.Substring(0, 5));
var characterPart = inputString[5];
if (characterPart == 'Z')
return string.Format("{0}{1}", (++integerpart).ToString("D5"), "A");
var nextChar = (char)(characterPart + 1);
return string.Format("{0}{1}", (integerpart).ToString("D5"), nextChar.ToString());
}