String Replace C# String Replace高效的实例方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

String Replace C# String Replace高效的实例方法

  2021-03-18 我要评论
想了解C# String Replace高效的实例方法的相关内容吗,在本文为您仔细讲解String Replace的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:String,Replace,下面大家一起来学习吧。
复制代码 代码如下:

[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);
        }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们