c# - 如何实现简单的字符串搜索

标签 c# .net winforms string search

我想根据我的搜索查询在我的应用程序中实现一个简单的搜索。 假设我有一个包含 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/

相关文章:

c# - 通过单击按钮使用带参数的存储过程在 asp.net winform 中填充 ListView

c# - 将帮助页面设置为默认路由

c# - C# 中的 Linux 键盘钩子(Hook)

C#:Base64编码

.net - 表单背景颜色设置为淡色

c# - 如何以编程方式调用 Windows 权限对话框?

c# - 对于每次迭代,在一个类中获得相同的值

c# - Ioc 中繁忙的构造函数——它们是代码的味道吗?

.net - 我在哪里可以找到好的 .NET PDF 库?

c# - 如果没有打开文件的程序,则捕获文件的打开