Reputation: 2207
I have a string which i need to increment by 1 The string has both characters and numeric values.
The string layout i have is as follows "MD00494"
How would i increment this to "MD00496" & "MD00497" ect
If it was a normal string with numbers i would parse it to an int.
I have tried the following
int i = int.Parse(sdesptchNo);
i++;
txtDispatchNo.Text = i.ToString();
Anyone any ideas how i would go about this.
Upvotes: 14
Views: 28457
Reputation: 73
The accepted answer does not work if there is a number in the middle of the string e.g. XXX123YYY456, exceptions are thrown.
I have written a generic method that will increment the end of the string and you can pass it the minimum amount of digits.
public static string IncrementStringEnd(string name, int minNumericalCharacters = 1)
{
var prefix = System.Text.RegularExpressions.Regex.Match(name, @"\d+$");
if (prefix.Success)
{
var capture = prefix.Captures[0];
int number = int.Parse(capture.Value) + 1;
name = name.Remove(capture.Index, capture.Length) + number.ToString("D" + minNumericalCharacters);
}
return name;
}
Test Results:
MD00494 : MD00495
XXX123YYY456 : XXX123YYY457
SD50MRF999 : SD50MRF1000
SD50MRF9 : SD50MRF010
For testing purposes https://dotnetfiddle.net/j1f6wh
Upvotes: 3
Reputation: 119
I use this to Increment/Decrement Barcodes
/// <summary>
/// Gets the number portion of the string and adds 1 to it
/// </summary>
public static string IncrementNumbers(this string numString)
{
if (numString.IsEmpty())
return numString;
else if (!numString.Where(Char.IsDigit).Any())
return numString;
else
{
string prefix = Regex.Match(numString, "^\\D+").Value;
string number = Regex.Replace(numString, "^\\D+", "");
int i = int.Parse(number) + 1;
return prefix + i.ToString($"D{numString.Length - prefix.Length}");
}
}
/// <summary>
/// Gets the number portion of the string and subtracts 1 from it
/// </summary>
public static string DecrementNumbers(this string numString)
{
if (numString.IsEmpty())
return numString;
else if (!numString.Where(Char.IsDigit).Any())
return numString;
else
{
string prefix = Regex.Match(numString, "^\\D+").Value;
string number = Regex.Replace(numString, "^\\D+", "");
int i = int.Parse(number) - 1;
return prefix + i.ToString($"D{numString.Length - prefix.Length}");
}
}
/// <summary>
/// Shortented IsNullOrWhiteSpace
/// </summary>
public static bool IsEmpty(this string str)
{
if (str.TrimFix() == null)
return true;
return false;
}
/// <summary>
/// Trims the String and returns Null if it's empty space
/// </summary>
public static string TrimFix(this string rawString)
{
if (!string.IsNullOrWhiteSpace(rawString))
{
return rawString.Trim();
}
return null;
}
Upvotes: 0
Reputation: 37
Here's 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();
}
Try it!
Upvotes: 0
Reputation: 354416
You first should figure out any commonality between the strings. If there is always a prefix of letters followed by digits (with a fixed width) at the end, then you can just remove the letters, parse the rest, increment, and stick them together again.
E.g. in your case you could 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 robust might be
var newString = Regex.Replace(x, "\\d+",
m => (int.Parse(m.Value) + 1).ToString(new string('0', m.Value.Length)));
This would replace any number in the string by the incremented number in the same width – but leaves every non-number exactly the same and in the same place.
Upvotes: 19
Reputation: 1
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);
Upvotes: 0
Reputation: 8937
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: This will save your zero
Upvotes: 0
Reputation: 223227
Here is one Non-Regex 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");
output would be:
newStr = "MD00495"
Upvotes: 9
Reputation: 498942
Assuming that you only need to increment the numeric portion of the string, and that the structure of the strings is always - bunch of non-numeric characters followed by a bunch of numerals, you can use a regular expression to break up the string into these two components, convert the numeric portion to an 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);
Upvotes: 5
Reputation: 4057
You need to find the position of the first digit in the string. Then split the string into 2 fields.
0 1 2 3 4 5 6
M D 0 0 4 9 4
The first field will be the non numeric part "MD" The second field will be the numeric part "00494"
Increment the numeric only part to "00495"
You will lose the leading zero's so you'll need to pad your new number with the same amount of zero's once you've incremented.
Then join the 2 fields.
Upvotes: 2