<span>中国美术大学 </span>          2009中国美术大学招生简章  
把前面的学校名字替换掉后面的标题中的字符 
学校名称是从数据库里取得,标题也是从数据库取得。 
效果如 
2009<span style=color:red>中国美术大学</span>招生简章
怎么做啊 
有没有这样一个函数?? 

动态实现替换字符串

解决方案 »

  1.   


    string title = "2009中国美术大学招生简章";
    string sname = "中国美术大学";title = title.Replace(sname,"<span style=color:red>"+sname+"</span>");//你要的结果
      

  2.   

    title.Replace(name,"<span style=color:red>"+name+"</span>")
      

  3.   

    把innerHTML里的内容改了再传进去不行吗?
      

  4.   

    如果是直接更新数据库 sql语句就行
    如果是显示时处理楼上的办法吧
      

  5.   

    string result=System.Text.RegularExpressions.Regex.Replace("你的字符串",@"<span>([^<]*)</span>\s*(.*?)\1","$2<span style=color:red>$1</span>");
      

  6.   

    如果不是标题,是里面的内容的话.先使用一个控件.
    页面部分放:
    <asp:Label ID="sname" runat="server"></asp:Label>.cs文件里写上:
    string s1 = "2009中国美术大学招生简章"; 
    string s2 = "中国美术大学";//数据库得到部分 sname.Text.Replace(s1," <span style=color:red>"+s2+" </span>");//你要的结果
      

  7.   

    string title = "2009中国美术大学招生简章"; 
    string School= "中国美术大学"; title = title.Replace(School," <span style=color:red>"+School+" </span>");
      

  8.   

    string str1 = "<span>中国美术大学</span>";
                string str2 = "2009中国美术大学招生简章";
                string str3=str2.Replace(str1.Replace("<span>","").Replace("</span>",""),str1.Replace("<span>","<span style=color:red>"));
      

  9.   


     string str1 = "<span>中国美术大学 </span>          2009中国美术大学招生简章 ";
     string str2 = Regex.Replace(str1, @"<span>(.*?)\s*</span>\s*(.*?)\1(.*?)", "$2<span style=color:red>$1</span>$3");
      

  10.   

    string name = name.replace("中国美术大学","<span style=color:red>中国美术大学</span>");
      

  11.   

    Replace 函数 或者用正则匹配