I need a solution that checks whether the content of a string of fixed lenght adheres to a set of rules. If not, I need to retrieve a list of the Rules that failed, the Expected value for each rule, and the Actually value contained within the string.
This is my current solution:
string actual = "628IDENTREGISTER153004085616P30062010EAPFEMPA013.1";
// Dictionary<Tuple<rule, expected>, startingPostion>
var expected = new Dictionary<Tuple<string, string>, int>
{
{new Tuple<string, string>("900052", "628"), 0},
{new Tuple<string, string>("9000250", "IDENTREGISTER1"), 3},
{new Tuple<string, string>("900092", "53004085616"), 17},
{new Tuple<string, string>("900004", "P"), 28},
{new Tuple<string, string>("900089", "30062010"), 29},
{new Tuple<string, string>("900028", "E"), 37},
{new Tuple<string, string>("900029", "A"), 38},
{new Tuple<string, string>("900002", "P"), 39},
{new Tuple<string, string>("900030", "FEMPA013.0"), 40}
};
// Create an IEnumerable of all broken rules
var result = expected.Where(field =>
!field.Key.Item2.Equals(
actual.Substring(field.Value, field.Key.Item2.Length)))
// Prints:
// [(900030, FEMPA013.0), 40]
foreach (var res in result)
Console.WriteLine(res);
I'm sure there’s a better way of solving this problem. Also, as it stands, I’m not entirely satisfied with this solution as it does not give me the actual field.
Thanks.