c#中增加字母数字ID的最好方法是什么?

本文关键字:方法 是什么 ID 增加 数字 | 更新日期: 2023-09-27 18:13:06

在c#中增加字母数字ID的最佳方法是什么?

例如

:

我们有345FAS310E575896325SA并且我们要增加123,所以我们得到结果:345FAS310E575896325SA123

或者345FAS310E575896325SA123加234,结果应该是345FAS310E575896325SA357

让它工作的"最便宜"的方法是什么?

c#中增加字母数字ID的最好方法是什么?

我的算法:

    static void Main(string[] args)
    {
        var id = "843342D4343DA123D";
        var intSummand = 10;
        Console.WriteLine(AddToStringId(id, intSummand));
        Console.ReadKey();
    }
    static string AddToStringId(string id, int summand)
    {                         
        // set the begin-pointer of for the number to the end of the original id
        var intPos = id.Length;
        // go back from end of id to the begin while a char is a number
        for (int i = id.Length - 1; i >= 0; i--)
        {
            var charTmp = id.Substring(i, 1).ToCharArray()[0];
            if (char.IsNumber(charTmp))
            {
                // set the position one element back
                intPos--;
            }
            else
            {
                // we found a char and so we can break up
                break;
            }
        }
        var numberString = string.Empty;
        if (intPos < id.Length)
        {
            // the for-loop has found at least one numeric char at the end
            numberString = id.Substring(intPos, id.Length - intPos);
        }
        if (numberString.Length == 0)
        {
            // no number was found at the and so we simply add the summand as string
            id += summand.ToString();
        }
        else
        {
            // cut off the id-string up to the last char before the number at the end
            id = id.Substring(0, id.Length - numberString.Length);
            // add the Increment-operation-result to the end of the id-string and replace
            // the value which stood there before
            id += (int.Parse(numberString) + summand).ToString();
        }
        // return the result
        return id;
    }

每个人都有一个问题,那就是你的字母数字值实际上没有任何意义。

当你给出例子时,你只是把数字加到末尾,然后加一,你并没有真正给我们任何关于字母代表什么的信息。

为了能够像这样增加一个值我们需要知道字母的值是什么,一个很好的例子是HEX 0 - 9a - F所以如果你要将HEX值09增加1就会得到0A 0F增加1就会得到10

我知道这不是一个答案,但直到你给我们一些真实的信息,你的目标是实现这个我们不能真正给出一个答案。也许还可以告诉我们你用这个做什么/为什么用字母,数字等?

通过看你的例子,我是这样理解的:

如果没有添加id后缀,则应该添加一个。否则,ID应该递增。

private static void Main(string[] args)
{
    var id = IncrementId("345FAS310E575896325SA", 123); // AS310E575896325SA123
    var id2 = IncrementId(id, 234); //345FAS310E575896325SA357
}
public static string IncrementId(string value, int id)
{
    // you might want to use fixed length or something else
    int suffixPos = value.IndexOf("SA");
    // no id has been appended
    if (value.Length == suffixPos + 2)
        return value + id;
    // increment the existing id.
    var currentId = int.Parse(value.Substring(suffixPos + 2));
    currentId += id;
    return value.Substring(0, suffixPos + 2) + currentId;
}

子字符串方法?当您传入希望增加的数字时,它将获得增加值的子字符串并将它们加在一起。

当增量超过99会发生什么?

它只是在字母数字ID的末尾添加100吗?

也将其余的字母数字ID保持不变?即:

843342 d4343da123d

10

843342 d4343da123d 20