我想根据我的搜索查询在我的应用程序中实现一个简单的搜索。 假设我有一个包含 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"};
如何根据上面提供的搜索查询获取第一篇文章?有什么想法吗?
最佳答案
这将返回 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
}
关于c# - 如何实现简单的字符串搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8366490/