0

If I have a string value like this "1234-", then I need to split till the non-numeric character that is - and add numeric value 1 after the non-numeric char. later I have to update the value to "1234-1". Then the program will check with the last updated value 1234-1 then it will increment by 1 every time and store it for future use. If no non-numeric in a string then the program will increment by 1 with the numeric string.

Below are some examples of String and Output Value

Ex Str1                           Output

2014-                             2014-1
2014-1                            2014-2
AAA                               AAA1
ABC-ABC                           ABC-ABC1
12345                             12346
1234AA                            1234AA1

I have used the below code before.

Code

var SiteFile = (from site in db.SiteFiles where site.Code == "ACQPONUM" select site.Line2).FirstOrDefault();     // Get Input string to generate AUTO number.
int Count = (from Porders in db.Porders where Porders.No.StartsWith(SiteFile) select Porders.No).ToList().Count;       // Get the count of matching values in db.
var PONo = (from Porders in db.Porders where Porders.No.StartsWith(SiteFile) select Porders.No).ToList();             // Get list of Matching existing values.
if (Count != 0)
{
if (PONo != null)
{
int Val = (from PONos in PONo let value = Regex.Match(PONos, @"\d+").Value select Convert.ToInt32(value == string.Empty ? "0" : Regex.Match(PONos, @"\d+").Value) + 1).Concat(new[] { 0 }).Max();     // Fiind the maximum value in the matched list nd Increment value by if same type exists in the db.
porder.No = SiteFile + Val.ToString();
}
}
else
{
   porder.No = SiteFile + "1";
}

Any help to this will be appreciated.

3
  • @Ulugbek Umirov: Whatever the values found at last then add +1 to the numeric value. If nothing found, then add 1 to that string. In your example the o/p should be 10. Commented Jun 9, 2014 at 4:31
  • What if you do it 10 times? What then? Commented Jun 9, 2014 at 5:20
  • A, A0, A1, A2 in database - what's the output? What if the order is different (you don't have any orderby), like A0, A2, A, A1? Commented Jun 9, 2014 at 7:10

2 Answers 2

1

Maybe something like this:

   string s = "123419";
   string res = null;
   char ch = s[s.Length - 1];
   if(char.IsDigit(ch)) // handle numbers
   {
      res = s.Substring(0,s.Length - 1);   
      string suffix = null;
       // special case
      if(ch == '9'){
         suffix = "10";
      }
      else
      {
         suffix = (++ch).ToString();
      }
      res += suffix;
   }
   else 
   {
      res = string.Format("{0}1", s);
   }
Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't it have problem with 1234-9? It should be transformed into 1234-10.
If my input strings is 1234-1234-1234 then I need to get the output as 1234-1234-1235. In this case, the current suggested code wont work.
Well, as I understand from comment of OP to my question (I deleted my question, but OP was fast), 9 is transformed into 10, and number (not digit) is transformed into number + 1, so if we have 999 in the end, it will be transformed into 1000, 12349 is transformed into 12350, etc.
I also need check for the existing records. Say like, I have input value AAA11AA. If db entry having string from AAA11AA1 to AAA11AA22, then I need the output of the current execution should be AAA11AA23.
@Ulugbek Umirov: I am sorry. Pl check my updated code.
0

Try this code:

private string Incrementvalue(string str)
{
    string retVal;
    if (str.Contains(DELIMITER))
    {
        string[] parts = str.Split(new char[] { DELIMITER }, 2);
        string origSuffix = parts[1];
        string newSuffix;

        int intSuffix;
        if (int.TryParse(origSuffix, out intSuffix))
            //Delimiter exists and suffix is already a number: Increment!                
            newSuffix = (intSuffix + 1).ToString();   
        else
            //Delimiter exists and suffix is NTO number: Add a "1" suffix.    
            newSuffix = origSuffix + 1;                

            retVal = parts[0] + DELIMITER + newSuffix;
    }
    else
    {

        int temp;
        if (int.TryParse(str, out temp))
        {
            //Delimiter does not exists and the input is a number: Increment last digit! 
            string newSuffix = (int.Parse(str[str.Length - 1].ToString()) + 1).ToString();
            retVal = str.Substring(0, str.Length - 1) + newSuffix;
            retVal = str.Substring(0, str.Length - 1) + newSuffix;
        }
        else
        {
            //Delimiter does not exists and the input is NOT a number: Add a "1" suffix. 
            retVal = str + "1";
        }        
    }
    return retVal;
}

The code could be written in a much more compact manner, but think this will be more readable and it will work...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.