在.NET中,不调用C++/CLI,进行字符串替换有好几种方法:

1、最常用的,就是String实例.Replace(),但这个不能忽略大小写。

2、System.Text.Regex(Regular Expression正则表达式),大家都估计到它的效率不高,虽然它支持忽略大小写。

3、String.SubString()循环,查找要替换的子字符串的位置,截取,然后字符串相加,大家也估计到,数量少(在codeproject.com上曾有文章讨论过和StringBuilder的临届值是600次)的情况下会比StringBuilder快。

4、跟3一样,唯一区别就是字符累加用StringBuilder,数量少的情况下比字符累加要慢,但过了临届值就要快。

5、引用Microsoft VisualBasic RunTime(Microsoft.VisualBasic.DLL),里面有一个Strings.Replace,效率非常高,其原理就是:Split()再Join(),其中Split支持忽略大小写的秘诀就是调用了System.Globalization.CultureInfo,也就是所谓的国际化,其实要实现字符串的替换代码量不多,但要兼容各种语言(非编程语言,是交流语言),那就得多花几倍的代码了。

6、不想用VB运行库的朋友,可以用Reflector配合Denis Bauer's Reflector.FileDisassembler把Microsoft.VisualBasic.DLL中的Strings的Replace和相关函数抽取出来(C#),然后修补一下就可以单独使用了(我这明显是吃饱了撑着,VB专家装配脑袋指出我这是浪费时间,因为本身Microsoft VisualBasic运行库就包括在.NET Framework中)。

实战

以下是测试代码:
        static void Main(string[] args)
        {
            string segment = "中华aBc共和国";
            string source;
            string pattern = "abc";
            string destination = "人民";
            string result = "";
            const long count = 1000;
            StringBuilder pressure = new StringBuilder();
            HiPerfTimer time;

for (int i = 0; i < count; i++)
            {
                pressure.Append(segment);
            }
            source = pressure.ToString();
   
            //regexp
            time = new HiPerfTimer();
            time.Start();
            for (int i = 0; i < count; i++)
            {
                result = Regex.Replace(source, pattern, destination, RegexOptions.IgnoreCase);
            }
            time.Stop();

Console.WriteLine("regexp    =" + time.Duration + ":");

//vb
            time = new HiPerfTimer();
            time.Start();
            for (int i = 0; i < count; i++)
            {
                result = Strings.Replace(source, pattern, destination, 1, -1, CompareMethod.Text);
            }
            time.Stop();

Console.WriteLine("vb        =" + time.Duration + ":");

//vbReplace
            time = new HiPerfTimer();
            time.Start();
            for (int i = 0; i < count; i++)
            {
                result = VBString.Replace(source, pattern, destination, 1, -1, StringCompareMethod.Text);
            }
            time.Stop();

Console.WriteLine("vbReplace =" + time.Duration + ":" + result);

//substring
            time = new HiPerfTimer();
            time.Start();
            for (int i = 0; i < count; i++)
            {
                result = StringHelper.ReplaceText(source, pattern, destination, StringHelper.CompareMethods.Text);
            }
            time.Stop();

Console.WriteLine("substring =" + time.Duration + ":");

//substring with stringbuilder
            time = new HiPerfTimer();
            time.Start();
            for (int i = 0; i < count; i++)
            {
                result = StringHelper.ReplaceTextB(source, pattern, destination, StringHelper.CompareMethods.Text);
            }
            time.Stop();

Console.WriteLine("substringB=" + time.Duration + ":");

Console.ReadLine();
        }

说明

这个代码演示了上述几种方法:要把字符串"中华aBc共和国"中的"abc"替换为"人民",注意:源子字符串是"aBc",要替换的是"abc",这里目的是要测试不区分大小写。

为了测试效率,我特意先把测试字符串累加1000次,然后循环测试1000次。

结果

以下是测试结果:
regexp      =1.38308285017339 //这是正则表达式,第3快;
vb            =0.525978828344589 //这是引用Microsoft VisualBasic RunTime的,次快;
vbReplace=0.522997341400086 //这就是用reflector改为C#的,最快;
substring  =21.8573638474698 //这是string.substring +,最慢
substringB=14.6346693500287 //这是string.substring StringBuilder,次慢,这里凸现了StringBuilder的速度;

这里仅仅是多次测试中的一次,我没有弄平均,大概数字吧,到底是vb快还是reflector的c#快,差不多...

是否应该使用Microsoft VisualBasic RunTime就见仁见智了。

来源:起点百问网(http://www.qdknow.com)[详细地址]:http://qdknow.com/Html/Article/View/2/188.html

转载于:https://www.cnblogs.com/fightLonely/archive/2010/04/08/1707296.html

C#不区分大小写的字符串替换(Replace)相关推荐

  1. C#不区分大小写的字符串替换(Replace)函数

    在.NET中,不调用C++/CLI,进行字符串替换有好几种方法: 1.最常用的,就是String实例.Replace(),但这个不能忽略大小写. 2.System.Text.Regex(Regular ...

  2. C++ STL string字符串替换 replace函数的使用

    //C++ string字符串替换 //replace()函数的使用 #include <iostream> #include <string> using namespace ...

  3. Python字符串替换replace、截取[]、查找find、计数count、分割split

    不同语言中replace用法不一样,有的是replace(string,old_word,new_word),有的是string.replace(old_word,new_word) 同样,截取等功能 ...

  4. python3 字符串替换 replace translate re.sub

    Python3的字符串替换,这里总结了三个函数,replace()和translate()和re.sub() replace() python 中的 replace() 方法把字符串中的 old(旧字 ...

  5. python数字替换成中文replace_Python3字符串替换replace(),translate(),re.sub()

    Python3的字符串替换,这里总结了三个函数,replace()和translate()和re.sub() replace() python 中的 replace() 方法把字符串中的 old(旧字 ...

  6. python字符串替换replace,Python字符串替换

    Python字符串替换 Python字符串替换教程 在 Python replace()函数详解 定义 replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第 ...

  7. JavaScript字符串替换replace方法

    在日常的js开发中, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replace('aa','dd') ...

  8. js字符串替换replace

    replace() 用来替换字符串中字符 //replace(old字符串,new字符串) // 字符串处理let aiqi = 'aiqi1?a'const a= aiqi.replace('?a' ...

  9. 替换字符串_面试题精选:字符串替换

    字符串处理在程序猿日常工作工作中非常常见,常见到几乎各种语言中都已经封装好了字符串相关的API,我们只需要直接拿过来用就好.就拿Java为例,jdk中的String()类几乎封装了所有字符串相关的操作 ...

  10. java 字符串格式替换_[Java] - 格式字符串替换方法

    Java 字符串格式替换方法有两种,一种是使用String.format(...),另一种是使用MessageFormat.format(...) 如下: import java.text.Messa ...

最新文章

  1. python maketrans方法
  2. requests.exceptions.TooManyRedirects: Exceeded 30 redirects
  3. 回文java_回文 Java
  4. 界面上下固定_基于ANSYS的胶粘结构界面开裂有限元计算
  5. IEWebBrowser组件的execWB方法
  6. mysql Error Code: 1005(errorno:121)解决
  7. 用户注意到用户计算机中千兆位网卡,为何你电脑上的千兆网卡跑不到千兆?
  8. 软键盘弹出时popwindow_【示例】解决软键盘弹出时底部元素上浮的问题
  9. 【Elasticsearch】action_request_validation_exception alidation Failed: 1: mapping type is missing
  10. python解析nginx配置文件_Nginx情景分析之配置文件解析
  11. centos 计算器_Linux学习之CentOS(十五)--Linux常用命令之bc、man、shutdown...
  12. 腾讯钟翔平:以数字技术驱动,做智慧交通共建者
  13. Vue.js安装方法
  14. Linux进程的管理与调度(七) -- Linux下2号进程的kthreadd
  15. 数据结构——单向循环链表双向循环链表
  16. 【人工智能】想要入坑机器学习?这是MIT在读博士的AI心得
  17. CText更新至V1.1.0
  18. linux查看占用负载的程序,Linux中查看负载
  19. 2022-2028全球与中国质地食品成分市场现状及未来发展趋势
  20. 《漫步华尔街》 读书笔记 part1 历史

热门文章

  1. Hive基本操作入门
  2. Prometheus入门简介
  3. spring-cloud Sleuth
  4. centos下docker无法正常启动检查与解决方法
  5. HDU 2256Problem of Precision(矩阵快速幂)
  6. MSSQL中Case语句的用法
  7. IOS学习之多线程(3)--线程安全
  8. Zen Cart对空间或主机的要求
  9. project 2013使用记录
  10. Java工程带库编译运行