c# - 搜索所有单词的 StartsWith 扩展

标签 c# linq search autosuggest startswith

是否有字符串的 StartsWith 扩展,它在字符串中的每个单词的开头进行搜索?

类似于: "Ben Stiller".StartsWithExtension("Sti")返回真值

我想要这个,这样我就可以为搜索做一个谓词。

假设有一个名为 Persons 的列表,ICollection<Person>
每个人都有一个属性名称,其值为“Ben Stiller”或“Adam Sandler”。

我希望能够像这样做谓词:

Persons.Where(p => p.Name.StartsWithExtension(query))

谢谢 (欢迎使用其他更好的方法来实现这一点)

最佳答案

您可以先按单词拆分字符串,如下所示:

var result = "Ben Stiller".Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                          .Any(x => x.StartsWith("Sti"));

当然你可以把它写成你自己的扩展方法,像这样:

public static bool AnyWordStartsWith(this string input, string test)
{
    return input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                .Any(x => x.StartsWith(test));
}

关于c# - 搜索所有单词的 StartsWith 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15722221/

相关文章:

c# - 将大括号用于可变范围目的是错误的吗?

c# - DocX 克隆表并在索引处插入

c# - 从自引用数据库表填充递归数据结构

Java 神谕搜索

c# - 使用文本框搜索 ListView 项目

c# - 在 .Net Remoting 中使用单例

c# - 比较两个字节数组以防止时序攻击

c# - 当记录应该是唯一的时重复记录

c# - 修改包装类的谓词表达式

python-搜索字典子列表;将字典键转换为值