How to implement a simple string search

I want to implement a simple search in my application based on the search query that I have. Let's say I have an array containing 2 paragraphs or articles, and I want to search in these articles for related topics or related keywords that I entered.

For instance:

//this is my search query
string mySearchQuery = "how to play with matches";

//these are my articles
string[] myarticles = new string[] {"article 1: this article will teach newbies how to start fire by playing with the awesome matches..", "article 2: this article doesn't contain anything"};

How can I get the first article based on the search query presented above? Any idea?

+5
source share
2 answers

This will return any string in myarticlesthat contains all the words in mysearchquery:

var tokens = mySearchQuery.Split(' ');
var matches = myarticles.Where(m => tokens.All(t => m.Contains(t)));

foreach(var match in matches)
{
    // do whatever you wish with them here
}
+6
source

, , .

"", , , 1 , ...

+1

All Articles