C#创建具有特定最后一个值的字符串序列
我正在尝试创建一个字符串格式的数字序列,一旦我达到“99999”,我想继续使用前导字母的序列,并将根据我在数据库中的最后一个值开始 示例序列:C#创建具有特定最后一个值的字符串序列,c#,C#,我正在尝试创建一个字符串格式的数字序列,一旦我达到“99999”,我想继续使用前导字母的序列,并将根据我在数据库中的最后一个值开始 示例序列: "00000" -> "00100" -> "99999" -> "A0001" -> "A9999" -> "B0001" -> "Z9999" -> "AA001" -> "ZZZZZ" 我有一个数据库,它的行有00001到99995个值 并且需要在最后一个数字99995的基础上递增 cmd.Comm
"00000" -> "00100" -> "99999" -> "A0001" -> "A9999" -> "B0001" -> "Z9999" -> "AA001" -> "ZZZZZ"
我有一个数据库,它的行有00001到99995个值
并且需要在最后一个数字99995
的基础上递增
cmd.CommandText = "SELECT id from Table ORDER BY id desc LIMIT 1"
string lastvalue = cmd.ExecuteScalar().ToString();
int quantity = 10;
total = lastvalue + quantity;
for (i = lastvalue; i < total; i++)
{
cmd.CommandText = "INSERT INTO Table VALUES ('"+i+"')";
cmd.ExecuteScalar();
}
找到了这个答案,但没有具体的开始和结束。它只是根据长度从开始到结束生成。十进制中所需的较低的4位数字? 这将是(数字%10000) 在Base 36系统中需要的上部。(10位+26个字符)
number
中的任何给定整数,也超过9999,您可以将其转换为:
public string FormatNumber(long number)
{
string result = (number % 10000).ToString();
number /= 10000;
while(number > 0)
{
int digit = number % 36;
if (digit < 10)
result = digit.ToString() + result;
else
result = (char)('A' + digit - 10) + result;
number /=36
}
return result;
}
公共字符串格式编号(长编号)
{
字符串结果=(数字%10000).ToString();
数目/=10000;
而(数量>0)
{
整数位数=数字%36;
如果(数字<10)
结果=数字.ToString()+结果;
其他的
结果=(字符)('A'+数字-10)+结果;
数目/=36
}
返回结果;
}
你的想法是什么?我建议你用十六进制保存第一个数字。我已经编辑过了,希望你能理解这是一个糟糕的想法,它会占用比数值更多的存储空间,甚至需要更多的处理时间(解析>递增>转换回字符串)。为什么不存储常规数值,然后在从数据库中读取数据并显示给用户时对其进行格式化?因为它将以串行方式打印。需要像这样扫描
public string FormatNumber(long number)
{
string result = (number % 10000).ToString();
number /= 10000;
while(number > 0)
{
int digit = number % 36;
if (digit < 10)
result = digit.ToString() + result;
else
result = (char)('A' + digit - 10) + result;
number /=36
}
return result;
}