views:

42

answers:

5

I have a list:

var myList = new List<string> { "red", "blue", "green" };

I have a string:

var myString = "Alfred has a red and blue tie";

I am trying to get a count of matches of words in myList within myString. Currently, I am using .Contains(), which gets me a count of 3 because it is picking up the "red" in "Alfred". I need to be able to osolate words instead. How can this be achieved?

var count = myList.Where(ml => myString.Contains(ml)); // gets 3, want 2
+1  A: 
var count = (from s in myList
            join ms in myString.Split() on s equals ms
            select new { s, ms }).Count();
Justin Niessner
A: 

Something like this?

var numMatches = myString.Split().Intersect(myList).Count();

Note that this doesn't consider duplicate occurrences.

If you do want to consider duplicates, go with @Justin Niessner's technique. Here's an alternative, with an intermediary lookup:

var words = myString.Split().ToLookup(word => word);
var numMatches = myList.Sum(interestingWord => words[interestingWord].Count());
Ani
A: 

this works \bred\b|\bblue\b|\bgreen\b I am not sure it is most optimized

Andrey
A: 

How about using Linq to Regex from this link?

Crassy
+3  A: 
        var myList = new List<string> { "red", "blue", "green" };
        Regex r = new Regex("\\b(" + string.Join("|", myList.ToArray()) + ")\\b");
        MatchCollection m = r.Matches("Alfred has a red and blue tie");

m.Count will give you the number of times red, blue or green are found. \b specifies word boundary.

Each element of m is of Type Match, and you can look at each index to get more info (ie m[0].Value gives you the matched string (red) and m[0].Index gives you the location in the original string (13)).

Mark Synowiec
Excellent answer, that would have been my answer as well +1
orvado
+1 - If I had any RegExFu whatsoever, I'd go with this answer because it will properly handle punctuation, etc.
Justin Niessner
@Mark - Is there a way to get the results of what is found. In this case a list containing "red","blue"? How about their index/positions relative to the original string? (Is this another question?)
Brian David Berman
@Brian - sure can, I updated the answer (m[x].Value and m[x].Index).
Mark Synowiec