在C#中将字符串从数字0-9递增到小写字母a-z到大写字母A-Z [英] Increment a string from numbers 0-9 to lowercase a-z to uppercase A-Z in C#
问题描述
我希望能够有一个以'000000'开头的长度为6个字符的字符串.然后当我打9时我想将其增加一个"000001",当我到达z时我想转到"00000a",我想转到"00000A".当达到"Z"时,我想将第一个重置为0,然后从下一个位置"000010"开始,依此类推. '000011','000012'...'0000a0','0000a1'...'0000A0','0000A1'
I want to be able to have a string 6 characters long starting with '000000'. Then I want to increment it by one '000001' when I hit 9 I want to go to '00000a' when I get to z I want to go to '00000A'. When 'Z' is reached I want to reset the first to 0 and start with the next position '000010' So on and so forth. '000011','000012'...'0000a0','0000a1'...'0000A0','0000A1'
我将如何在C#中执行此操作?
How would I do this in C#?
先谢谢您. 迈克
推荐答案
This uses the IntToString
supporting arbitrary bases from the question Quickest way to convert a base 10 number to any base in .NET?, but hardcoded to use your format (which is base 62).
private static readonly char[] baseChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private const long targetBase = 62;
private const long maxNum = 62L*62*62*62*62*62 - 1;
public static string NumberToString(long value)
{
if (value > maxNum)
throw new ArgumentException();
char[] result = "000000".ToCharArray();
int i = result.Length - 1;
do
{
result[i--] = baseChars[value % targetBase];
value /= targetBase;
}
while (value > 0);
return new string(result);
}
这篇关于在C#中将字符串从数字0-9递增到小写字母a-z到大写字母A-Z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!