本人正实现数据搜索功能,发现一难题:
  如何在搜索到的内容中给搜索关键字加颜色。
不胜感激!!

解决方案 »

  1.   

    写一函数,如下
    public static string GetColorString(string old,string key)
    {
    char[] sp={' ','|','\'',';',':','-','(',')'};
    string[] arrkey=key.Split(sp);
    foreach(string temp in arrkey)
    {
    if(temp!="")
    {
    Match m=Regex.Match(old,temp,RegexOptions.IgnoreCase);
    if(m.Success)
    {
    string str_m=m.Value;
    old=old.Replace(str_m,"<span class=a>"+str_m+"</span>");
    }
    } }
    return old; }
    参数old位要显示的字符,key位搜索的字符
      

  2.   

    class=a 就可以设置颜色,直接用color=red也可以
      

  3.   

    see:http://search.csdn.net/Expert/topic/2112/2112690.xml?temp=.3626215
      

  4.   

    在输出的时候直接把关键字替换一下就OK呀
    string result;
    ……
    result=result.Replace(keyword,"<font color='red'>"+keyword+"</font>");