日期:2014-05-19  浏览次数:20395 次

|M| Replace替换是替换全部的,现在我想替换第一个怎么替换
如12341234
我想替换第一次出现的1234为4321
结果为
43211234

------解决方案--------------------
没有现成的函数 先IndexOf -> Remove-> Insert
------解决方案--------------------
string a = "12341234 ";
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex( "1234 ");
Response.Write(r.Replace(a, "4321 ",1,0));
Response.End();
------解决方案--------------------

------解决方案--------------------
自己封装个函数吧
public string ReplaceFirst(string strSrv,string strFind,string strNew)
{
int firstpos = strSrv.IndexOf(strFind);
strSrv.Remove(firstpos, strFind.Length);
strSrv.PadLeft(strNew);
return strSrv;
}
------解决方案--------------------
string str = "12341234 "; Regex reg = new Regex( "1234 "); str = reg.Replace(str, "4321 ", 1); Response.Write(str);
------解决方案--------------------
孟子的这个方法不错