简体   繁体   English

如何检查字符串是否包含列表/数组中的任何字符串

[英]How to check if String contains any of the strings in List/Array

I currently have a an ASP.Net Website. 我目前有一个ASP.Net网站。 This website retrieves RSS-Feeds from different Sources and outputs its own RSS-Feed . 该网站从不同来源检索RSS-Feed,并输出自己的RSS-Feed But is the article contains some word they must be 'blacked out' (Censorship as you might say). 但是文章是否包含某些词,必须将其“涂黑”(您可能会说审查制度)。 The words are contained in a blacklist managed by that ASP.Net Website. 这些单词包含在该ASP.Net网站管理的blacklist

My current solution: 我当前的解决方案:

foreach (SyndicationItem rssItem in syndFeed.Items.OrderByDescending(x => x.PublishDate).ToList())
{
    //if is on blacklist
    bool isValid = true;
    foreach (String blacklistItem in MyBlacklist)
    {
        if(rssItem.Title.Text.contains(blacklistItem))
            isValid = false;
        if(rssItem.Summary.Text.contains(blacklistItem))
            isValid = false;
    }

    if (isValid)
    {
        writer.WriteStartElement("item");

        //Write Elements
        writer.WriteElementString("PubDate", rssItem.PublishDate.ToString("yyyy-MM-dd HH:mm:ss"));
        writer.WriteElementString("title", rssItem.Title.Text);
        writer.WriteElementString("description", rssItem.Summary.Text);
        writer.WriteElementString("link", rssItem.Id.ToString());

        writer.WriteEndElement();
    }
}

Question: Is there a more performant way to do this search? 问题:是否有更高性能的搜索方法? How can i write the following code more simple using LINQ/ Any other Stringoperations? 如何使用LINQ /其他任何Stringoperations更简单地编写以下代码?

    foreach (String blacklistItem in MyBlacklist)
    {
        if(rssItem.Title.Text.contains(blacklistItem))
            isValid = false;
        if(rssItem.Summary.Text.contains(blacklistItem))
            isValid = false;
    }

Based on the following examples from Java: 基于Java的以下示例:

Checking if a string contains any of the strings in an array 检查字符串是否包含数组中的任何字符串

How to check string with array of strings in java? 如何在Java中使用字符串数组检查字符串?

Test if a string contains any of the strings from an array 测试字符串是否包含数组中的任何字符串

bool isValid = !myBlackList.Any(s=> rssItem.Title.Text.contains(s) || rssItem.Summary.Text.Contains(s))

要么

bool isValid = myBlackList.All(s=> !rssItem.Title.Text.Contains(s) && !rssItem.Summary.Text.Contains(s))

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

相关问题 如何使用 Linq 检查字符串列表是否包含列表中的任何字符串 - How to use Linq to check if a list of strings contains any string in a list 如何检查字符串是否包含某些字符串 - How to check if a String contains any of some strings 如何知道一个字符串是否包含列表中的任何字符串? - how to know if a string contains any of the strings on a list? 如何检查字符串数组包含特定的字符串? - How to check Array of strings contains a particular string? 如何检查字符串是否包含实体框架中列表中的任何字符串? - How do you check if a string contains any strings from a list in Entity Framework? 如何使用 Linq where 条件检查字符串列表是否包含任何字符串 - How to use Linq where condition to check if a list of strings contains any string 如何检查字符串是否包含字符串数组中的字符串? - How do I check if a string contains a string from an array of strings? 如何确定字符串是否包含字符串列表的任何匹配项 - How to determine if a string contains any matches of a list of strings 如何查找字符串是否包含字符串列表的任何项目? - How to find if a string contains any items of an List of strings? 如何检查字符串是否包含List的任何元素 <string> ? - How to check if a string contains any element of a List<string>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM