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 woul
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.
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);
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.
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"
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;
}
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