简体   繁体   中英

Split numeric and Non-Numeric in a String

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.

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM