简体   繁体   English

C#字符串搜索带有多个子字符串

[英]C# String Search with multiple substrings

So I'm working on a filter for a resource list and one of the filters is the Name property(string). 因此,我正在为资源列表创建一个过滤器,其中一个过滤器是Name属性(字符串)。

As a(dumb) example : resource name is "Big,Red/Square Table" and the filter is "Table Red", it should be a valid resource 作为一个(哑)示例:资源名称为“大红色/方桌”,过滤器为“红色桌”,它应该是有效的资源

This is what I could come up with using the little time I had: 这是我用很少的时间就能想到的:

static void ApplyNameFilter(ref ApplicationViewModel model, string filter)
{
    if (string.IsNullOrEmpty(filter) || filter == "") return;
    char[] separators = {' ', ',', '.', '/', '\\', '|', '_', '-'};
    var validResources = new List<ResourceModel>();

    foreach (var resource in model.ResourcesViewModel.Resources)
    {
        var filterSubstrings =
            filter
            .ToLower()
            .Split(separators)
            .ToList();

        var resourceSubstrings =
            resource.Name
            .ToLower()
            .Split(separators)
            .ToList();

        resourceSubstrings.ForEach(substring => {
            if (filterSubstrings.Contains(substring))
                filterSubstrings.RemoveAll(sub => sub == substring);
        });

        if (filterSubstrings.Count == 0)
            validResources.Add(resource);
    }

    model.ResourcesViewModel.Resources = validResources;
}

Should I take a different approach for this? 我应该采取其他方法吗?

EDIT: Ended up going with this until I figure out RegEx 编辑:结束与此直到我找出正则表达式

static void ApplyNameFilter(ref ApplicationViewModel model, string filter)
{
    if (string.IsNullOrEmpty(filter)) return;

    char[] separators = {' ', ',', '.', '/', '\\', '|', '_', '-'};
    var filterSubstrings =
        filter
            .ToLower()
            .Split(separators)
            .ToList();

    var validResources = model.ResourcesViewModel.Resources
        .Where(resource => filterSubstrings.All(fs => resource.Name.ToLower().Contains(fs)))
        .ToList();

    model.ResourcesViewModel.Resources = validResources;
}    

You can use LINQ to make this more concise (and probably faster, as you are creating List s and removing elements over every resource unnecessarily, though a non-LINQ solution could be even faster). 您可以使用LINQ使其更简洁(可能更快,因为您正在创建List并不必要地删除每个资源上的元素,尽管非LINQ解决方案甚至可以更快)。

var validResources = model.ResourcesViewModel.Resources
                        .Where(resource => {
                            var resourceSubstrings = resource.Name.ToLower().Split(separators).ToHashSet();
                            return filterSubstrings.All(fs => resourceSubstrings.Contains(fs));
                        })
                        .ToList();

If you are willing to accept all the filter substrings being inside the Name regardless of the separators , then you can simplify to just be: 如果你愿意接受所有的过滤器正在子里面Name不管的separators ,那么你就可以简化为仅仅是:

var validResources = model.ResourcesViewModel.Resources
                        .Where(resource => filterSubstrings.All(fs => resource.Name.ToLower().Contains(fs)))
                        .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM