搜索结果格式化


        private string FormatContext(string SearchKeyWords, string AllWords)
        {
            string ResultWorlds = string.Empty;
            AllWords = Regex.Replace(Regex.Replace(AllWords, @"\[pic\].*?\[/pic\]", "", RegexOptions.IgnoreCase), @"<[^>]*>", "", RegexOptions.IgnoreCase).Trim();
            int WordsCount = AllWords.Length;
            if (AllWords.Contains(SearchKeyWords))
            {
                if (WordsCount <= 100)
                {
                    ResultWorlds = Regex.Replace(ResultWorlds, SearchKeyWords, "<em>" + SearchKeyWords + "</em>", RegexOptions.IgnoreCase);
                }
                else
                {
                    ResultWorlds = AllWords.Substring(AllWords.IndexOf(SearchKeyWords));
                    if (ResultWorlds.Length > 100)
                    {
                        ResultWorlds = ResultWorlds.Substring(0, 97) + "...";
                        ResultWorlds = Regex.Replace(ResultWorlds, SearchKeyWords, "<em>" + SearchKeyWords + "</em>", RegexOptions.IgnoreCase);
                    }
                }
            }
            else
            {
                if (WordsCount <= 100)
                {
                    ResultWorlds = AllWords;
                }
                else
                {
                    ResultWorlds = AllWords.Substring(0, 97) + "...";
                }
            }

            return ResultWorlds;
        }

原文地址:https://www.cnblogs.com/stulife/p/1783635.html