查找并显示包围的文本并突出显示搜索词MVC 5

时间:2017-10-26 10:51:27

标签: c# asp.net-mvc linq

我需要的是返回高亮的字符串搜索词(searchString)以及前10个单词和下10个单词(例如)单词。我不想显示所有内容。我想我必须使用indexOf或RegExp。

(我已经尝试过这个,直到现在,我设法显示所有内容并高亮显示里面的搜索词。)

控制器:

var result = (from c in db.sitecontent
                        where c.ContentText.Contains(searchString)
                        orderby c.ContentID
                        select new
                        {
                            ContentText = c.ContentText
                        }).ToString();


ViewData["searchTerm"] = searchString;

var mres= result.IndexOf(searchString) + 20;
var finalr= result.Substring(mres, result.IndexOf(searchString) + mres);
ViewData["searchR"] = finalr;
return View(result);

查看:

@Html.Raw((ViewData["searchR"].ToString().Replace((string)ViewData["searchTerm"], "<span style='color:red'>" + (string)ViewData["searchTerm"] + "</span>")))

谢谢

1 个答案:

答案 0 :(得分:1)

我没有检查所有步骤,但首先,在计算子字符串时遇到问题。

  • 对于开始索引,您必须确保实际上可以选择10 之前的字符。
  • 对于您的总长度,您还必须检查您的 字符串足够大。

以下是计算此值的实用方法。

ngOnChanges

我用自动测试测试它看起来很好。 对于您的特定情况,要获取结果,请在linq查询上调用ToList()(而不是字符串)。它为您提供了所有结果。 循环这些结果与下面的例子类似。

public static string GetPreview(string searchString, int previewLength, string paragraph)
{
    var searchedStringLength = searchString.Length;
    var searchedStringPosition = paragraph.IndexOf(searchString);

    // Index from where you start your substring
    //  - In nominal case : index of your searched string - your length for preview
    //                  That is : searchedWordPosition - previewLength
    //  - If your string starts with your searched word, then index must be 0 and not previewLength
    //  - If your string has less than preview characters before your searched word then it must be a 0 index as well
    var leftPartLength = Math.Min(previewLength, searchedStringPosition);
    var startIndex = searchedStringPosition - leftPartLength;

    // Then you need to know how many characters you'll take from your starting point
    // You'll obviously take all from left index to searchedWordPosition : leftIndex + paragraph.Length
    // Then you need to pick maxmimum previewLength characters if possible
    // Otherwise, you'll pick what's left from the end of your searched word to the end of the string, 
    // that is your total string length -  searched word length - index of your seach word
    // To summarize. If your string is shorter than you think (5 chars only after your searched word)
    //      you'll get remaining chars that is : paragraph.Length - leftIndex - searchString.Length
    // But if your string is larger, simply pick 10 + your searchstring length :)

    // Total length is : left part length + word length + right part left
    var rightPartLength = Math.Min(previewLength, paragraph.Length - (searchedStringPosition + searchedStringLength));
    var length = leftPartLength + searchedStringLength + rightPartLength;

    // So your substring for preview is :
    var preview = paragraph.Substring(startIndex, length);

    return preview;
}