Csharp/C#教程:C# String Replace高效的实例方法分享

上述就是C#学习教程:C# String Replace高效的实例方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)! 代码如下:
[ThreadStatic]
       staticchar[]mTempChars;
       protectedstaticchar[]GetTempData()
       {
           if(mTempChars==null)
               mTempChars=newchar[1024*64];
           returnmTempChars;
       }
       publicstaticstringReplace(stringvalue,stringoldData,stringnewData)
       {
           char[]tmpchars=GetTempData();
           intnewpostion=0;
           intoldpostion=0;
           intlength=value.Length;
           intoldlength=oldData.Length;
           intnewlength=newData.Length;
           intindex=0;
           intcopylength=0;
           booleq=false;
           while(index<value.Length)
           {
               eq=true;
               for(intk=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;
 &n bsp;             }
               else
               {
                   index++;
               }
           }
           if(oldpostion<length)
           {
               copylength=index-oldpostion;
               value.CopyTo(oldpostion,tmpchars,newpostion,copylength);
               newpostion+=copylength;
           }
           returnnewstring(tmpchars,0,newpostion);
       }
您可能感兴趣的文章:javascript模拟实现C#String.format函数功能代码C#实现去除Strings中空格的方法C#中string和StingBuilder内存中的区别实例分析C#中String与string的区别分析C#自定义函数NetxtString生成随机字符串C#中把字符串String转换为整型Int的小例子C#中StringBuilder类的使用

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/904634.html

(0)
上一篇 2021年10月22日
下一篇 2021年10月22日

精彩推荐