Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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'.

C#
string strInput = "    X       ";           // String from form (this is 4 spaces followed by X followed by n spaces
string pattern = "0,4,6,8,11";              // String containing matching positions of X in string

string place = strInput.IndexOf("X").ToString();        // Get position of X
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.
Posted

1 solution

Hi Annie!

Try this.

C#
string strInput = "    X       "; // String from form (this is 4 spaces followed by X followed by n spaces
string pattern = "0,4,6,8,11"; // String containing matching positions of X in string

string place = strInput.IndexOf("X").ToString();        // Get position of X (4)
string[] positions = pattern.Split(",");

//Create a temporary structure saving the positions along with their positions.
var result = positions.Select((s, i) => new { Pos = i, Str = s }) 
            .Where(o => o.Str == place) //Pick the item with a position matching the input
            .FirstOrDefault();

if (result != null)
{
    var output = result.Pos;
}


EDIT: This solution is much more succinct, but note that it requires the input to always be valid, so you might want to do some initial checking.

C#
string strInput = "    X       ";           // String from form (this is 4 spaces followed by X followed by n spaces
string pattern = "0,4,6,8,11";              // String containing matching positions of X in string

int place = strInput.IndexOf("X");        // Get position of X (4)
var positions = pattern.Split(",").Select(s => int.Parse(s));

int result = positions.TakeWhile((p, i) => p != place)
                      .Count();
 
Share this answer
 
v2
Comments
[no name] 3-Apr-11 15:09pm    
Thanks, so much, Jonathan. Works a charm. Now I've got some learning to do!
Annie
[no name] 3-Apr-11 16:26pm    
Oh, I really like the second one. If there is no match the result value is greater than positions.Length, so I'll just check for that. Neat. Thanks again. Annie

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900