Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string contains a list of substrings and save the matching ones

This is my situation: I have a string representing a text

string myText = "Text to analyze for words, bar, foo";   

And a list of words to search for in it

List<string> words = new List<string> {"foo", "bar", "xyz"};

I'd want to know the most efficient method, if exists, to get the list of the words contained in the text, something like that:

List<string> matches = myText.findWords(words)
like image 385
accand Avatar asked May 15 '15 13:05

accand


1 Answers

There is no special analysis in this query except you have to use Contains method. So you may try this:

string myText = "Text to analyze for words, bar, foo";

List<string> words = new List<string> { "foo", "bar", "xyz" };

var result = words.Where(i => myText.Contains(i)).ToList();
//result: bar, foo
like image 156
Hossein Narimani Rad Avatar answered Oct 19 '22 23:10

Hossein Narimani Rad