Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET LINQ
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 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 3-Apr-11 8:15am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Annie!
 
Try this.
 
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.
 
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();
  Permalink  
v2
Comments
AnnieMacD at 3-Apr-11 15:09pm
   
Thanks, so much, Jonathan. Works a charm. Now I've got some learning to do!
Annie
AnnieMacD at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 288
1 Maciej Los 260
2 _Amy 230
3 Manfred R. Bihy 200
4 CHill60 180
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,177
2 Maciej Los 3,774
3 Peter Leow 3,478
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 3 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100