Sol*_*ake 5 .net c# string search winforms
我想根据我的搜索查询在我的应用程序中实现一个简单的搜索.假设我有一个包含2个段落或文章的数组,我想在这些文章中搜索我输入的相关主题或相关关键字.
例如:
//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"};
Run Code Online (Sandbox Code Playgroud)
如何根据我上面提供的搜索查询获得第一篇文章?任何的想法?
这将返回myarticles
包含以下所有单词的任何字符串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
}
Run Code Online (Sandbox Code Playgroud)