简体   繁体   English

如何<strong>从C#中的字符串中</strong>删除双HTML <strong>标记</strong>

[英]How to remove double html <strong> tag from string in C#

I want to made search on my ASP.NET MVC 3 site, so for search I have to find matched patterns and in that words replace matched part with the same part in bold(I us efor that html <strong> tag). 我想在我的ASP.NET MVC 3站点上进行搜索,因此对于搜索,我必须找到匹配的模式,即用粗体将相同的部分替换为相同的部分(我使用html <strong>标记)。

So I have this in my controller 所以我在控制器中有这个

        string[] words=content.Split(' ');
        foreach (Thread thread in context.Threads)
        {
            foreach (string word in words)
            {
                if (thread.Title.ToLower().Contains(word.ToLower()))
                {
                    thread.Title=Regex.Replace(thread.Title,word,String.Format("<strong>{0}</strong>","$0"),RegexOptions.IgnoreCase);
                }
            }

         }

So, if I search new thread a It will find thread like this New thrEAd . 因此,如果我搜索一个new thread a ,它将找到类似New thrEAd线程。

But in html it makes my string like that 但是在html中,它使我的字符串像这样

<strong>New</strong> <strong>thrE<strong>A</strong>d</strong>

So I want to remove strong tag from a, because it makes a double-bold... How can I do that? 因此,我想从中删除强标签,因为它会使标签变成双粗体...我该怎么做?

And if you have interesting ways to do my search, I will glad to hear your suggestions too. 如果您有有趣的搜索方法,我也会很高兴听到您的建议。

您可以通过检查搜索词中是否包含其他任何词来清理搜索词:

var cleanWords = words.Where(w => !words.Any(w2 => w2.Contains(w));

Try this: 尝试这个:

IEnumerable<string> enumerableWords = content.Split(' ').Distinct();
string[] words = enumerableWords.ToArray();
foreach (Thread thread in context.Threads) {
    string result = thread.Title;
    foreach (string word in words) {
        result = Regex.Replace(result, String.Format(@"\b{0}\b", word), String.Format(@"<strong>{0}</strong>", word), RegexOptions.IgnoreCase);
    }
    thread.Title = result;
}

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

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