I get two strings as input from two separate sources (over which I have no control). The first string
strInput
will contain one 'X', the position of which should be equal to the value of an element in the
pattern
string. I need the
index of the element in
pattern
.
I've got it working in regular code using 'for' loops but would like to use LINQ and/or lambdas to make it more succinct. The following returns the value 4, but what I need is the index, which in this case is '1'.
string strInput = " X ";
string pattern = "0,4,6,8,11";
string place = strInput.IndexOf("X").ToString();
string[] positions = pattern.Split(',');
var thisIndex = from pos in positions
where (pos == place)
select pos.First();
foreach (var x in thisIndex)
{
Console.WriteLine("This is the index of X: {0}", x);
Console.ReadLine();
}
Any help would be appreciated.