我目前是这样写的:sContent = sRs["Content"].ToString().Replace("爱情", "<a href='http://www.sgw520.com/aqwz.aspx'>爱情</a>").Replace("感情", "<a href='http://www.sgw520.com/qgwz.aspx'>感情</a>");但是文章中出现多个 “爱情”这个关键字,都被替换了,十分不美观,现在我想只替换文中第一个出现的关键字就行了,求方法啊,跪谢~`

解决方案 »

  1.   

    string s = "你好,感情的问题是需要有感情的人采用有感情的方式来处理的。";
    Regex regex = new Regex("(?s)感情");
    string r = regex.Replace(s, "(替换后的字符串)", 1);
    Response.Write(r);
      

  2.   


    string sContent = sRs["Content"].ToString();
    int index = sContent.IndexOf("爱情");
    sContent = sContent.Remove(index,"爱情".Length).Insert(index, "<a href='http://www.sgw520.com/aqwz.aspx'>爱情</a>");先移除,再插入。用Regex.Replace()用正则表达式应该也可以实现,但在下对正则不是很熟悉。