源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C# String Replace高效的实例方法

  • 时间:2022-07-17 00:37 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C# String Replace高效的实例方法
[u]复制代码[/u] 代码如下:
[ThreadStatic]         static char[] mTempChars;         protected static char[] GetTempData()         {             if (mTempChars == null)                 mTempChars = new char[1024 * 64];             return mTempChars;         }         public static string Replace(string value, string oldData, string newData)         {             char[] tmpchars = GetTempData();             int newpostion = 0;             int oldpostion = 0;             int length = value.Length;             int oldlength = oldData.Length;             int newlength = newData.Length;             int index = 0;             int copylength = 0;             bool eq = false;             while (index < value.Length)             {                 eq = true;                 for (int k = 0; k < oldlength; k++)                 {                     if (value[index + k] != oldData[k])                     {                         eq = false;                         break;                     }                 }                 if (eq)                 {                     copylength = index - oldpostion;                     value.CopyTo(oldpostion, tmpchars, newpostion, copylength);                     newpostion += copylength;                     index += oldlength;                     oldpostion = index;                     newData.CopyTo(0, tmpchars, newpostion, newlength);                     newpostion += newlength;                 }                 else                 {                     index++;                 }             }             if (oldpostion < length)             {                 copylength = index - oldpostion;                 value.CopyTo(oldpostion, tmpchars, newpostion, copylength);                 newpostion += copylength;             }             return new string(tmpchars, 0, newpostion);         }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部