[英]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.