以下是代码片段:/// <summary> /// /// </summary> /// <param name="str">待处理的字符串</param> /// <param name="toRep">要替换的字符串中的子串</param> /// <param name="strRep">用来替换toRep字符串的字符串</param> /// <returns>返回一个结果字符串</returns> public static string StringReplace(string str, string toRep, string strRep) { StringBuilder sb = new StringBuilder(); int np = 0, n_ptmp = 0; for (; ; ) { string str_tmp = str.Substring(np); n_ptmp = str_tmp.IndexOf(toRep);
if (n_ptmp == -1) { sb.Append(str_tmp); break; } else { sb.Append(str_tmp.Substring(0, n_ptmp)).Append(strRep); np += n_ptmp + toRep.Length; } } return sb.ToString(); }
Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved. 闵涛 E_mail:admin@mintao.net(欢迎提供学习资源)
鄂公网安备 42011102001154号
站长:MinTao ICP备案号:鄂ICP备11006601号-18