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

发布于 2024-12-12 13:29:47 字数 315 浏览 5 评论 0原文

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

例如:

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

或者我们< 345FAS310E575896325SA123并增加 234 ,结果应该是 345FAS310E575896325SA357

什么是使其工作的“最便宜”方法?

What is a best way to increment alphanumeric ID by certain value in C#?

For example:

We've 345FAS310E575896325SA and we're going to increment by 123 , so we have as result: 345FAS310E575896325SA123

Or we've 345FAS310E575896325SA123 and increments by 234 , and result should be 345FAS310E575896325SA357

What is a "cheapest" way to make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半山落雨半山空 2024-12-19 13:29:47

这是我的算法:

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

This is my algorithm:

    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;
    }
熟人话多 2024-12-19 13:29:47

每个人都遇到的问题是您的字母数字值实际上没有任何意义。

当您给出示例时,您只是在末尾添加数字并递增这些数字,您并没有真正向我们提供有关字母代表什么的任何信息。

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

我知道这不是答案,但在您向我们提供一些关于您希望通过此实现的目标的真实信息之前,我们无法真正给出答案。也许还可以告诉我们您用它做什么/为什么使用字母数字等?

The problem everyone is having here is that your Alphanumeric value doesnt really mean anything.

When you give your examples you are just adding numbers onto the end and incrementing those, you havent really given us any information about what the letters represent.

To be able to increment a value like this we need to know what the value of the letters are, a good example would be HEX, 0 - 9 A - F so if you were to say increment the HEX value 09 by 1 you would get 0A and incrementing 0F by 1 gives 10

I know this isnt an answer but until you give us some real info on what you are aiming to achieve with this we cant really give an answer. Also maybe tell us what you are using this for / why AlphaNumeric etc?

望她远 2024-12-19 13:29:47

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

如果没有 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;
}

By looking at your examples, I interpret it like this:

If no ids are suffixed, one should be appended. Else, the ID should be incremented.

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;
}
莫言歌 2024-12-19 13:29:47

子字符串方法?在您传入要增加的数字的地方,它会获取增加值的子字符串并将它们加在一起?

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

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

另外,字母数字 ID 的其余部分会保持不变吗?即:

843342D4343DA123D 10

843342D4343DA123D 20

A Substring method? Where you pass in the number you wish to increment by, it will get the substring of the incremented values and add them together?

What happens when the increment goes over 99?

Does it just append 100 to the end of the alphanumeric ID?

Also will the rest of the alphanumberic ID stay the same? i.e:

843342D4343DA123D 10

843342D4343DA123D 20

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文