All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
十八 链接的可用性_[Html教程]
PHP程序加速探索之代码优化_[PHP教程]
ASP.NET Remoting体系结构(六)
关于ASP.NET在IIS一些问题的经验总结_[Asp.Net教程]
.NET基础知识-.NET框架_[Asp.Net教程]
SQL Server定时作业的设置方法_[SQL Server教程]
.NET基础知识-目标、战略_[Asp.Net教程]
一定要远离毒品,网页恶意脚本代码大总结_JavaScript技术_编程技术
.net组件开发系列(三)之武术系列-控件生命周期与控件事件机制_[Asp.Net教程]
Asp.net,Ajax-我可以用javascript做些什么?_[Asp.Net教程]
Reading number is top 10 pictures
美丽的少女1
西班牙山村小景5
中国文革时期的色情图片2
美女浴室写真3
陪睡门马睿菈自曝写真 称首拍大尺度照片3
Absolutely shocked. National geographic 50 animal photographys2
Sora aoi mirror memorial classics4
福利是必须的
The hot big eye big breast beauty3
Is said to be a Chinese female artist fame explicit pictures before2
Download software ranking
功夫熊猫2(上集)
传奇私服架设教程
WebService在.NET中的实战应用教学视频 → 第1集
Boxer's Top ten classic battle9
WebService在.NET中的实战应用教学视频 → 第3集
Photoshop 8.0图象编辑软件
ASP.NET.2.0.XML.高级编程(第3版)
Unix video tutorial2
Unix video tutorial5
SP4 for SQL2000
归海一刀 published in(发表于) 2014/1/30 1:21:59 Edit(编辑)
asp.net搜索关键词高亮显示函数_[Asp.Net教程]

asp.net搜索关键词高亮显示函数_[Asp.Net教程]

asp.net搜索关键词高亮显示函数_[Asp.Net教程]
在搜索关键词高亮中一般的方法都是采用替换的办法(Replace)这个方法有一个缺点就是不能区分大小写的问题。在网上找了找发现有人用正则表达式的方法来解决这个问题还真不错,效率也比较高,归纳如下,有用得到的朋友可以一试。
//搜索关键词高亮显示函数
public static string HighLightKeyWord(string pain,string keyword)
{
//搜索关键词高亮函数By JN 2006.11.30
System.Text.RegularExpressions.MatchCollection m = Regex.Matches(pain, keyword, RegexOptions.IgnoreCase);
//忽略大小写搜索字符串中的关键字
for (int j = 0; j < m.Count; j++)//循环在匹配的子串前后插东东
{
//j×31为插入html标签使pain字符串增加的长度:
pain = pain.Insert((m[j].Index + keyword.Length + j * 31), "");//关键字后插入html标签
pain = pain.Insert((m[j].Index + j * 31), "");//关键字前插入html标签
}
//搜索关键词高亮函数By JN 2006.11.30
return pain;
}
当然用之前引用先:using System.Web.UI.HtmlControls;
还有:using System.Text.RegularExpressions;(小鱼加)
以上代码有问题:同一句中有多个关键字时出问题
试一下这个先
///
/// 替换关键字为红色
///

/// 原始内容
/// 关键字,支持多关键字
/// String
/// haver Guo
public static string Highlightkeywords(string keycontent, string k)
{
string resultstr = keycontent;
if (k.Trim().IndexOf(’ ’) > 0)
{
string[] myArray = k.Split(’ ’);
for (int i = 0; i < myArray.Length; i++)
{
resultstr = resultstr.Replace(myArray[i].ToString(), "" + myArray[i].ToString() + "");
}
return resultstr;
}
else
{
return resultstr.Replace(k, "" + k + "");
}
}
经测,可用


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.