asp.net高亮显示搜索关键字的两个方法
方法一、完成查找关键字字体高亮显示 string serkeys(string , string)#region 方法一、完成查找关键字字体高亮显示 string serkeys(string , string)
方法二、完成查找关键字字体高亮显示,精确到每个字符#region 方法二、完成查找关键字字体高亮显示,精确到每个字符
- /**//// <summary>
- /// 完成查找关键字字体高亮显示
- /// </summary>
- /// <span name="str" class="mceItemParam"></span>字符串</param>
- /// <span name="sqlkey" class="mceItemParam"></span>字符串关键词</param>
- /// <returns>返回字符串</returns>
- public static string serkeys(string str,string sqlkey)
- {
- //sqlstr 待查找的关键字
- //str 已经查找到带有关键字的字符串
- int len=sqlkey.Length;
- int len1=str.Length;
- string onesqlstr,rproname="";
- rproname=str;
- //去掉关键字头和尾的空格,及将关键词
- sqlkey=sqlkey.Trim();
- if (sqlkey.IndexOf(" ")>0)
- {
- string [] kewArray=sqlkey.Split(' ');
- rproname=str;
- if (kewArray.Length>0)
- {
- for(int i=0;i<=kewArray.Length-1;i++)
- {
- onesqlstr =kewArray[i];
- if(onesqlstr!="" && onesqlstr!=null)
- {
- if( str.IndexOf(onesqlstr)>=0)
- rproname = rproname.Replace(onesqlstr,"<span style="color:#ff0033" mce_style="color:#ff0033">"+onesqlstr+"</span>");
- }
- }
- }
- }
- else
- {
- onesqlstr=sqlkey;
- if( str.IndexOf(onesqlstr)>=0)
- rproname = rproname.Replace(onesqlstr,"<span style="color:#ff0033" mce_style="color:#ff0033">"+onesqlstr+"</span>");
- }
- return rproname;
- }
方法二、完成查找关键字字体高亮显示,精确到每个字符#region 方法二、完成查找关键字字体高亮显示,精确到每个字符
- /**//// <summary>
- /// 完成查找关键字字体高亮显示,精确到每个字符
- /// </summary>
- /// <span name="str" class="mceItemParam"></span>字符串</param>
- /// <span name="sqlkey" class="mceItemParam"></span>字符串关键词</param>
- /// <returns>返回字符串</returns>
- public static string serkeys_char(string str,string sqlkey)
- {
- //sqlstr 待查找的关键字
- //str 已经查找到带有关键字的字符串
- int len=sqlkey.Length;
- int len1=str.Length;
- string onesqlstr,pproname,rproname="";
- string [] memo= new string[len1];
- for(int j=0;j<=len1-1;j++)
- {
- onesqlstr=sqlkey;
- pproname=str.Substring(j,1);
- if (onesqlstr.ToLower().IndexOf(pproname.ToLower())>=0)
- {
- memo[j]="<span style="color:#ff0033" mce_style="color:#ff0033">"+pproname+"</span>";
- }
- else
- {
- memo[j]=pproname;
- }
- }
- for(int i=0;i<=memo.Length-1;i++)
- {
- rproname+=memo[i];
- }
- return rproname;
- }