Increase a string using letters and numbers.

I have a string that I need to increase by 1. The string has both characters and numeric values.

The format of the string that I have is as follows: "MD00494"

How would I increase this to "MD00496" and "MD00497" ect

If it was a normal string with numbers, I would parse it with int.

I tried the following

int i = int.Parse(sdesptchNo); i++; txtDispatchNo.Text = i.ToString(); 

Any ideas how I will do this.

+6
source share
7 answers

You must first find out some commonality between the lines. If at the end there is always a prefix of letters followed by numbers (with a fixed width), you can simply delete the letters, analyze the rest, increment and reconnect them.

eg. in your case, you can use something like the following:

 var prefix = Regex.Match(sdesptchNo, "^\\D+").Value; var number = Regex.Replace(sdesptchNo, "^\\D+", ""); var i = int.Parse(number) + 1; var newString = prefix + i.ToString(new string('0', number.Length)); 

Another option that might be a little more reliable might be

 var newString = Regex.Replace(x, "\\d+", m => (int.Parse(m.Value) + 1).ToString(new string('0', m.Value.Length))); 

This will replace any number in the string with an incremental number of the same width, but leaves all the odd numbers exactly the same and in the same place.

+12
source

Assuming that you only need to increase the number of the string and that the structure of the lines is always a bunch of non-numeric characters followed by a bunch of numbers, you can use the regular expression to break the string into these two components, convert the number to integer, increment and then concatenate back.

 var match = Regex.Match("MD123", @"^([^0-9]+)([0-9]+)$"); var num = int.Parse(match.Groups[2].Value); var after = match.Groups[1].Value + (num + 1); 
+3
source

You need to find the position of the first digit in the string. Then divide the line into 2 fields.

0 1 2 3 4 5 6

MD 0 0 4 9 4

The first field will be the non-digital part of "MD" The second field will be the numerical part of "00494"

The increment of the numeric part only to "00495"

You will lose the initial zero, so you will need to put your new number with the same zero as soon as you increase it.

Then attach 2 fields.

+2
source

Here is one unrebutable way: P

 string str = "MD00494"; string digits = new string(str.Where(char.IsDigit).ToArray()); string letters = new string(str.Where(char.IsLetter).ToArray()); int number; if (!int.TryParse(digits, out number)) //int.Parse would do the job since only digits are selected { Console.WriteLine("Something weired happened"); } string newStr = letters + (++number).ToString("D5"); 

:

 newStr = "MD00495" 
+2
source

You can use regex:

 int kod = int.Parse(Regex.Replace(sdesptchNo, "[^0-9]", "")) + 1; string zeroStr=Regex.Replace(sdesptchNo, "[^0-9]", ""); string newZeroStr=""; for (int x=0;x<zeroStr.length;x++) if (zeroStr[x]=='0') newZeroStr=newZeroStr+"0"; else break; string newVal=Regex.Replace(sdesptchNo, "[0-9]", "") + newZeroStr + kod; 

UPDATED: it will save your zero

0
source

Here is my solution:

 string str = Console.ReadLine(); string digits = new string(str.Where(char.IsDigit).ToArray()); string letters = new string(str.Where(char.IsLetter).ToArray()); string newStr; int number; if (!int.TryParse(digits, out number)) { Console.WriteLine("Something weird happened"); } if (digits.StartsWith("0")) { newStr = letters + (++number).ToString("D5"); } else { newStr = letters + (++number).ToString(); } 

Give it a try!

0
source
 string sDispatchNo = "MS00914"; var pattern = @"^[a-zA-Z]+"; var strPart = Regex.Match(sDispatchNo, pattern).Value; var noPart = Regex.Replace(sDispatchNo, pattern, ""); var no = int.Parse(noPart); var length = noPart.Length; length = (no + 1)/(Math.Pow(10,length)) == 1 ? length + 1 : length; var output = strPart + (no + 1).ToString("D" + length); 
0
source

All Articles