Search code examples
c#stringsearch-engine

How can you check if a list of strings match all the words in your phrase?


I have a list of strings

string[] arr = new string[] { "hello world", "how are you", "what is going on" };

and I need to check if the string I give uses every word in one of the strings in arr

so let's say I have

string s = "hello are going on";

it would be a match because all of the words in s are in one of the strings in arr

string s = "hello world man"

this one would not be a match because "man" is not in any of the strings in arr

I know how to write a "longer" method to do this but is there a nice linq query that I can write?


Solution

  • string[] arr = new string[] { "hello world", "how are you", "what is going on" };
    string s = "hello are going on";
    string s2 = "hello world man";
    bool bs = s.Split(' ').All(word => arr.Any(sentence => sentence.Contains(word)));
    bool bs2 = s2.Split(' ').All(word => arr.Any(sentence => sentence.Contains(word)));