C# String Replace高效的实例方法

时间:2023-10-26 23:24:08 


[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);
        }
标签:String,Replace
0
投稿

猜你喜欢

  • 基于Android实现百度地图定位过程详解

    2021-06-12 20:55:34
  • 10个C#程序员经常用到的实用代码片段

    2022-12-01 13:02:58
  • Java 反射机制详解及实例代码

    2023-07-13 15:22:29
  • Java中实现可拖放图片剪裁入门教程

    2022-04-23 12:11:03
  • Java 导出excel进行换行的案例

    2021-07-29 04:09:36
  • 使用springboot aop来实现读写分离和事物配置

    2022-10-29 11:43:07
  • Android判断和监听底座状态和类型的方法介绍

    2022-03-27 09:33:30
  • idea创建maven父子工程导致子工程无法导入父工程依赖

    2021-09-17 09:34:08
  • Java 全面系统介绍反射的运用

    2021-12-18 22:51:30
  • java使用jdbc链接Oracle示例类分享

    2022-06-25 19:18:58
  • 详解Java动态字节码技术

    2022-06-20 03:20:20
  • Android开发应用中Broadcast Receiver组件详解

    2023-04-25 09:35:35
  • Seata AT模式启动过程图文示例详解

    2022-12-04 19:24:56
  • Java中的SuppressWarnings注解使用

    2023-08-18 17:31:19
  • Android shell命令行中过滤adb logcat输出的方法

    2023-11-23 07:30:29
  • java web个人通讯录系统设计

    2022-09-14 11:12:15
  • Spring Security和Shiro的相同点与不同点整理

    2023-01-15 17:07:20
  • Java Web实现文件下载和乱码处理方法

    2022-03-14 20:28:38
  • 使用Android造了个滚轮控件轮子示例

    2023-04-29 07:09:17
  • Java Springboot 重要知识点整理汇总

    2022-03-17 01:06:57
  • asp之家 软件编程 m.aspxhome.com