-2

I have two variables 2016-V-0049 and 2016-V-0070. Is there a way in which I can compare them and get all the missing data between them while comparing the last numbers. So in this case I want the result to be 2016-V-0050,2016-V-0051,2016-V-0052...etc.

Also can I have repeatable patterns like comparing 2016P13 and 2016P25(NumberWordNumber) and getting the missing numbers in between.

Sorry for not including what I have tried. Here's what I did and which works. I was just looking for more patterns which are generic.

string s = "2016-s-89";
string p = "2016-s-95";
var start = Convert.ToInt32(Regex.Match(s, @"\d+$").Value) +1;
var end = Convert.ToInt32(Regex.Match(p, @"\d+$").Value);
var index = Regex.Match(s, @"\d+$").Index;
string data = s.Substring(0, index);
List<string> newCases = new List<string>();
while (start < end)
{
    string newCaseNumber = string.Format("{0}{1}", data, start);
    newCases.Add(newCaseNumber);
    start++;
}
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
SP1
  • 1,182
  • 3
  • 22
  • 47
  • 3
    Please read [ask] and share what you have tried. The first part of your problem is getting the numbers from the string, the second part is incrementing the variable holding the number. Both are documented thoroughly on the web and this site. – CodeCaster Sep 26 '16 at 18:27
  • 1
    What would be the missing values between 2015-V-50 and 2016-V-60 ? – Trifon Sep 26 '16 at 18:29
  • 2
    "Is there a way in which I can compare them and get all the missing data between them while comparing the last numbers" -- no, I don't think you'll find an existing API that does this as it sounds far too specific; but yes, of course "there is a way" by writing very specific code to do exactly what you need with exactly the type of strings you are expecting. If you want help with that code then write some first. E.g., `string.Substring()`, `regex.IsMatch()`, `int.Parse()` to start working with your strings. – Quantic Sep 26 '16 at 18:30

2 Answers2

0

You should use substring() function and convert to integer like this code:

    var min = "2016-V-0049";
    var max = "2016-V-0070";

    var a = min.Substring(7);
    var b = max.Substring(7);

    int convertableVariable1 = int.Parse(a);
    int convertableVariable2 = int.Parse(b);

    for (int i = convertableVariable1; i < convertableVariable2; i++){
          Console.WriteLine("2016-V-{0:0000}",i);
    }


   Console.WriteLine("Difference :{0}", convertableVariable2 - convertableVariable1);
Husni Salax
  • 1,968
  • 1
  • 19
  • 29
0

Once you can define step by step what you actually want your code to do, the implementation is trivial to research and put together. In steps, you want this:

  1. Parse the input string denoting the starting number, so you can obtain the numeric part you're interested in.
  2. Now you have a numeric string, but it's still a string. Parse it to an integer so you can later perform arithmetic operations on it, such as incrementing it by one.
  3. Repeat 1 & 2 for the string denoting the ending number.
  4. Loop over the numbers between the start and end number, and rebuild the original string with the new number.

A naive implementation to do that looks like this:

string inputStart = "2016-V-0049";
string inputEnd = "2016-V-0070";

string pattern = @"[0-9]{4}\-[A-Z]{1}\-([0-9]{4})";

var regex = new Regex(pattern);

var match = regex.Match(inputStart);
var numberStart = int.Parse(match.Groups[1].Value);

match = regex.Match(inputEnd);
var numberEnd = int.Parse(match.Groups[1].Value);

for (int currentNumber = numberStart + 1; currentNumber < numberEnd; currentNumber++)
{
    Console.WriteLine("2016-V-{0:0000}", currentNumber);
}       

But this doesn't do input checking (start < end, start and end actually conforming to the pattern), doesn't support different patterns (the pattern and rebuild string are hardcoded) and assumes the "2016-V-" part of the string to always be the same.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272