日期:2014-05-17  浏览次数:20486 次

只替换文章中第一个关键字,求解
我目前是这样写的:

sContent = sRs["Content"].ToString().Replace("爱情", "<a href='http://www.sgw520.com/aqwz.aspx'>爱情</a>").Replace("感情", "<a href='http://www.sgw520.com/qgwz.aspx'>感情</a>");

但是文章中出现多个 “爱情”这个关键字,都被替换了,十分不美观,现在我想只替换文中第一个出现的关键字就行了,求方法啊,跪谢~`

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

------解决方案--------------------
C# code

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>");

------解决方案--------------------
探讨

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