-6

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?

Slaine
  • 41
  • 1
  • 9
  • 5
    In your first example, you're not incrementing, you're appending. Is that normal? – MPelletier Oct 31 '11 at 14:08
  • so are you talking about only changing the last set of digits? add 40 to your last example so now you have `843342D4343DA123D100`? – Eonasdan Oct 31 '11 at 14:10
  • Can you add some more details about what rules this ID field has? for instance if you have D10 and increment by 99 what happens? do you get D109 or do you get something like E09 etc? – Purplegoldfish Oct 31 '11 at 14:10
  • You can do either, but why don't you make up your mind before you ask for two solutions? – C.Evenhuis Oct 31 '11 at 14:10
  • ahh...i'm sorry. Edited. – Slaine Oct 31 '11 at 14:10
  • @Jesse Can you be more specific as In 1st case 843342D4343DA123D -> 843342D4343DA123D10 ( appending ) in 2nd, 843342D4343DA123D10 -> 843342D4343DA123D60 ( adding by 50) what if i again add by 50 -> 843342D4343DA123D110 ( adding by 50) will this do ? – Pratik Oct 31 '11 at 14:10
  • What you want to do makes no sense. In your first example the string should be 843342D4343DA123D0 if 843342D4343DA123D10 were to make any sense. What if the string was actually 843342D4343DA123**10** instead what would happen? You need to think of another way to represent your ID because your current implementation makes no sense. – Security Hound Oct 31 '11 at 14:13
  • 4
    your example makes less sense then it did before – Eonasdan Oct 31 '11 at 14:13
  • After your edit, it makes even less sense. How do you go from `345FAS310E575896325SA` to `345FAS310E575896325SA123` by adding 10? – Greg Oct 31 '11 at 14:13
  • 2
    You're still appending. Try this: Explain your algorithm. Even if it's a bad one, it's fine. – MPelletier Oct 31 '11 at 14:14
  • 1
    Maybe clarifying will help - are you looking to add "decimal 10" to "hex 843342D4343DA123D" or append the string "10"? – Aaron Anodide Oct 31 '11 at 14:17
  • You are basically asking how to perform base-36 math, but in that case your example numbers are too large for simple bigint conversion and calculation. – tawman Oct 31 '11 at 14:21

4 Answers4

4

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;
    }
Alexander Schmidt
  • 5,631
  • 4
  • 39
  • 79
  • Thats simply not true. My code checks for a number with any length at the end of the id and will increase it if it finds a number. If the string ends with a char, it will simply append the bypassed id. So the assignment is accomplished! Did you tried out my code, Ramhound? – Alexander Schmidt Oct 31 '11 at 14:27
  • Please describe what your code does and maybe add a couple comments. – Greg Oct 31 '11 at 14:43
  • 1
    He at least provides some kind of solution, and if you test it, it actually works as he mentions; if the string ends with a number it increases, if not it appends. +1 – Hanlet Escaño Oct 31 '11 at 14:50
  • Just looking on code: it simply parse the string from End, and figures out if there is a number at the end of Alphanumeric value. If NOT: it just APPENDS integer string to Alphanumeric one. If YES, it creates an integer from Alphanumeric end-substring, adds integer, and CONCATENATES to result string. +1 for providing solution. – Tigran Oct 31 '11 at 14:53
1

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?

Purplegoldfish
  • 5,268
  • 9
  • 39
  • 59
  • Sorry, but I don't get the problem here. I understand the question. All he wants to do is to add some value to the last complete number inside a string. If no number is given at the end (string ends with a nun-numeric char), he wants to interprete it as 0. The example shows it very good!?!? Or am I just wrong? – Alexander Schmidt Oct 31 '11 at 14:36
  • 1
    You cant just increment an alphanumeric value by adding on as many numbers as you want to the end, that isnt a true alphanumeric value its just a random string of letters and numbers, He hasnt explained what values the letters represent etc. For instance what if i do A357 +1,000,000 ? Does this just become A1000357? What is A? What is the numeric diference between A1 and B1 etc? The idea of alphanumeric values is that it actually represents something and isnt just random – Purplegoldfish Oct 31 '11 at 14:43
  • @sprinter252 If that is true, then there's an obvious flaw in that algorithm: it'll hit some character limit fast. – MPelletier Oct 31 '11 at 14:43
  • @MPelletier you migth be right but I just can't get the tons of downquotes because Jesse has got a very special problem and searches for a fitting solution. I think, char-limits are not that important in this case. I'm just confused about the reactions! – Alexander Schmidt Oct 31 '11 at 14:58
  • 1
    @sprinter252 its not the fact that he is looking for an answer that is getting downvotes, its that the question isnt understandable with the info provided. Consider this In his example he uses A and B, which have to have a value, so if A+1 = A1 what does n have to be here A + n = b? this is the big downfall of his question and without this info no one can accurately answer it – Purplegoldfish Oct 31 '11 at 15:08
  • 1
    @sprinter252 Well, there's three things going on. First, there's the mob rule reaction the community had. That should have been handled much better. The mob must have scared that poor poster away... Second, there's a grave flaw in the question which should/could have been more clear. Third, there's a series of underlying flaws in the problem, that increment =/= append, and limit cases (what happens after 99, what happens below 10) and other points, like how often can this be done, is this really a good design, etc? – MPelletier Oct 31 '11 at 17:23
  • @MPelletier - He asked a confusing question, never provided the details, and then never came back. The questio cannot be answered. – Security Hound Nov 01 '11 at 11:45
1

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;
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • if you look at his question before the edit the last set of characters changed. the OP is also setting this up as `345FAS310E575896325SA` is an existing "ID" so the next "ID" should be `345FAS310E575896325SA123` but as others have pointed out `345FAS310E575896325SA` is just a string from the OP example – Eonasdan Oct 31 '11 at 14:53
  • Works fine but you have to know, what you are searching for ('SA'). – Alexander Schmidt Oct 31 '11 at 14:53
  • @sprinter252 that's what I was trying to get at. The original example was `843342D4343DA123D` – Eonasdan Oct 31 '11 at 15:01
  • hence my comment `you might want to use fixed length or something else`. – jgauffin Oct 31 '11 at 15:06
0

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

stuartmclark
  • 1,203
  • 13
  • 22