Click here to Skip to main content
15,907,874 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
i have string like A02540 or A02540a or 00A2052255c025 or 00501C2326 or 00801 , i want to increase any of them or any another string one by one (auto increment) to become :
A02541 or A02540a1 or 00A2052255c026 or 00501C2327 or 00808

What I have tried:

string intPart = string.Join("", serialTXT.Reverse().TakeWhile(char.IsDigit).Reverse());
Int64 intP = Int64.Parse(intPart);
string newString = serialTXT.Remove(serialTXT.Length - intPart.Length) +
            (intP < 10 ? "0" + (intP + endnumber) : (intP + endnumber).ToString());


but it ignore "zero" in this case 00801 and convert it to 801
Posted
Updated 22-Sep-19 19:53pm
Comments
Richard MacCutchan 22-Sep-19 12:18pm    
if the string representing a number contains leading zeroes, then you need to count them before doing your conversion. You can then add them back into the new string that you create.
Golden Basim 22-Sep-19 15:26pm    
but this issue don't happen with any string contains Letter like (00501C2326) , how to determine that
Richard MacCutchan 23-Sep-19 1:31am    
You need to create an algorithm that can work for every number type. If part of it is kept as a string then the leading zeros will be preserved. But if the entire string is numeric and you convert it to an integer, then the leading zeros will be lost.
[no name] 22-Sep-19 21:35pm    
What about aa9?
Patrice T 22-Sep-19 21:58pm    
Are you sure about 00801 => 00808 ?
What is next for A99 ?

There are some edge cases you need to define your strategy for:

1 what to do with leading zeroes : xxxx09 should become xxxx10 or xxxx0010

2 xx23yyy or 23xxx : xx24yyy or 24xxx ?

3 what if the string contains no digits ?

Here's a "starting point:" none of the edge cases mentioned above are handled, but the number of leading zeroes are calculated ... I have left places for you to figure out what to do:
C#
public string IncString(string input, int inc = 1)
{
    Char ch = input.Last();
    
    if (Char.IsDigit(ch))
    {
        StringBuilder sb = new StringBuilder();
        
        sb.Append(ch);
        
        int leadingzeroes = 0;
        
        int length = input.Length;
        
        for (int i = length - 2; i >= 0; i--)
        {
            ch = input[i];
        
            if (Char.IsDigit(ch))
            {
                sb.Insert(0, ch);
            }
            else
            {
                break;
            }
        }
        
        string snum = sb.ToString();
        
        leadingzeroes = snum.TakeWhile(chr => chr == '0').Count();
        
        int num = Int32.Parse(sb.ToString()) + inc;
        
        sb.Clear();
        
        // sb.Insert(?);
        
        // if (leadingzeroes > 0) sb.Append(?);
        
        // sb.Append(?);
        
        return sb.ToString();
    }
    
    return string.Empty;
}
 
Share this answer
 
Comments
Golden Basim 23-Sep-19 9:58am    
hi, thanks for your answer :
1 what to do with leading zeroes : xxxx09 should become xxxx10 or xxxx0010
xxxx09 should become xxxx10

2 xx23yyy or 23xxx : xx24yyy or 24xxx ?
no it should become xx23yyy1 , 23xxx2 ,also xx23yyy99 should become xx23yyy100

3 what if the string contains no digits ?
Abcdf should become Abcdf0

how to achieve that ?
BillWoodruff 23-Sep-19 10:02am    
all the pieces you need are right there in the code example : study, experiment, learn
[no name] 23-Sep-19 17:01pm    
+5 room for improvement, but nicely done
Hi,

I would not rely on numeric types for this job, just operate on characters:

- use three state variables: bool carry (initially false), bool insideFirstNumber (initially false), and int countNumbers (initially zero);

- scan the string right to left;

- while finding digits:
    - if insideFirstNumber is false, set carry true, increment countNumbers, 
      and if it was zero, then and only then set insideFirstNumber true;
    - if insideFirstNumber and carry both true, emit incremented digit and clear carry,
      unless it was a "9", then emit "0" and set carry true;
    - otherwise just copy the digit.

- while finding non-digits but also when reaching start of string:
    - clear insideFirstNumber;
    -if carry is true, emit a "1" (number gets extra digit e.g. 99 -> 100) and clear carry;
    - copy the non-digit (not at start of string!).

All this would take about 20 lines of code, nothing fancy, no LINQ, no lambdas, just one StringBuilder object.

:)


PS: the above would turn A02540a into A02541a; if you really want to get A02540a1, then small adjustments are required, things become even simpler, as now insideFirstNumber and carry would initially be true, and countNumbers would not be needed at all.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900