在C#中通过特定值增加字母数字ID的最佳方法是什么?

时间:2011-10-31 14:05:57

标签: c# .net increment alphanumeric

在C#中将字母数字ID增加一定值的最佳方法是什么?

例如:

我们 345FAS310E575896325SA ,我们将增加123,因此我们得到结果: 345FAS310E575896325SA123

或者我们 345FAS310E575896325SA123 并增加234,结果应为 345FAS310E575896325SA357

什么是“最便宜”的方式让它发挥作用?

4 个答案:

答案 0 :(得分:4)

这是我的算法:

    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;
    }

答案 1 :(得分:1)

每个人在这里遇到的问题是你的字母数字值并不意味着什么。

当你给出你的例子时,你只是在最后添加数字并递增数字,你还没有给我们任何关于字母代表什么的信息。

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

我知道这不是一个答案,但是在你给我们一些关于你想要实现的目标的真实信息之前,我们无法给出答案。也许可以告诉我们你使用这个/为什么使用AlphaNumeric等?

答案 2 :(得分:1)

通过查看您的示例,我将其解释为:

如果没有后缀,则应追加一个。否则,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;
}

答案 3 :(得分:0)

一个子串方法?在传递要增加的数字的位置,它将获得递增值的子字符串并将它们一起添加?

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

是否只将100附加到字母数字ID的末尾?

其余的alphanumberic ID也会保持不变吗?即:

843342D4343DA123D 10

843342D4343DA123D 20

相关问题