您的位置:首页 > 其它

截取关键字高亮作为搜索结果

2015-09-25 11:29 489 查看
if (dtt.Rows.Count > 0)
            {
                int count = 0;
                foreach (DataRow dr in dtt.Rows)
                {
                    count++;
                    int tid = Convert.ToInt32(dr["TId"].ToString());
                    string title;
                    string content;
                        title = dr["PTitle"].ToString();
                        content=dr["Content"].ToString();
                        int startIndex = content.IndexOf(key);
                        if (content.Length > 22)
                        {
                            string contentBefore = dr["Content"].ToString().Substring(startIndex - 22>0?startIndex - 22:0, 22);
                            string contentAfter = dr["Content"].ToString().Substring(startIndex + key.Length,22);
                            content = "..." + contentBefore + key + contentAfter + "...";
                        }
                        sb.Append("<li  class=\"text-overflow\"><dl><dt><a href=\"" + HttpContext.Current.Request.Url.AbsolutePath + "q_" + tid + ".html\">" + title + "</a></dt><dd>" + content + "</dd></dl></li>");

                }
这边截取的是关键字左边22和右边22的内容放在Ul和li嵌套的Dl,dt里面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: