You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

前2位字母后7位数字的连续字母数字串生成问题排查

字母数字序列生成需求及现有实现

需求说明

  • 生成格式:前两位为字母,后七位为数字的字母数字串,按顺序递增
  • 序列递推规则:当序列到达AA9999999后,需自动切换为AB0000001继续递增,而非生成A10000000
  • 附加功能:支持批量生成;需将最后生成的计数存储到数据库,保证Windows服务重启后可读取该计数,继续生成新序列

现有实现代码

初始化及类型选择代码

private static List<char> Values;

private static readonly char[] AlphaNumericValues = {
    'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z','2','3', '4', '5', '6', '7', '8', '9'
};
private static readonly char[] NumericValues = {
    '0','1','2','3', '4', '5', '6', '7', '8', '9'
};

private static readonly char[] AlphaOnlyValues = {
    'A', 'B', 'C', 'D', 'E','F', 'G', 'H','I', 'J', 'K', 'L', 'M', 'N','O' ,'P', 'Q', 'R', 'S', 'T','U', 'V', 'W', 'X', 'Y', 'Z'
};
private static int ValueCount = 0;



switch (selectedItem.suffixType)
{
    case "alphanumeric":
        Values = AlphaNumericValues.ToList();
        ValueCount = AlphaNumericValues.Count();
        break;
    case "numeric":
        Values = NumericValues.ToList();
        ValueCount = NumericValues.Count();
        break;
    case "alphaonly":
        Values = AlphaOnlyValues.ToList();
        ValueCount = AlphaOnlyValues.Count();
        break;
}

if(selectedItem.suffixType== "alphaonly")
{
    GeneratedNumbers = GenerateValues2(KnoLength, selectedItem).ToList();
}

生成方法代码

private IEnumerable<Tuple<string, long>> GenerateValues2(int count, PrefixItems selectedItem)
{
    for (var c = 0; c < count; c++)
    {
        yield return FormatNumber2(KeyCountCurrent++, selectedItem.PrefixSize);

    }

}
private static Tuple<string, long> FormatNumber2(long value, int FormatSize)
{

    var digits = Enumerable.Repeat(Values[0], FormatSize).ToList();

    digits[2] = NumericValues[Convert.ToInt32(0)];
    digits[3] = NumericValues[Convert.ToInt32(0)];
    digits[4] = NumericValues[Convert.ToInt32(0)];
    digits[5] = NumericValues[Convert.ToInt32(0)];
    digits[6] = NumericValues[Convert.ToInt32(0)];
    digits[7] = NumericValues[Convert.ToInt32(0)];
    digits[8] = NumericValues[Convert.ToInt32(0)];


    var slotCount = digits.Count;
    var current = value;
    var count = 0;
    while (current > 0)
    {
        long rem;
        current = Math.DivRem(current, NumericValues.Count(), out rem);
        digits[slotCount - ++count] = NumericValues[Convert.ToInt32(rem)];
    }

    return new Tuple<string, long>(item1: string.Join("", digits), item2: value);
}

内容的提问来源于stack exchange,提问作者rahularyansharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 14:50:33