欢迎来到天天文库
浏览记录
ID:12059931
大小:29.50 KB
页数:7页
时间:2018-07-15
《五种在.net中字符串替换的方法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、五种在.NET中字符串替换的方法五种在.NET中字符串替换的方法来源:68design.net 2007年09月16日09:28 网友评论:0条 点击:18631:使用String.Replace函数替换,但不支持大小写。2:正则System.Text.Regex替换,用RegExpOption修改是否支持大小写。3:在小数据的情况下,使用String.SubString和+可以实现间接替换。4:导入MicrosoftVisualBasicRunTime(Microsoft.VisualBasic.DLL)使用Strings.Re
2、place速度很快。5:参照反射Reflector.FileDisassembler配合Strings.SplitandStrings.Join等实现,速度同5。一下介绍一种算法,类似KMP算法。有兴趣的参照研究下。privatestaticstringReplaceEx(stringoriginal,stringpattern,stringreplacement){intcount,position0,position1;count=position0=position1=0;stringupperString=original
3、.ToUpper();stringupperPattern=pattern.ToUpper();intinc=(original.Length/pattern.Length)*(replacement.Length-pattern.Length);char[]chars=newchar[original.Length+Math.Max(0,inc)];while((position1=upperString.IndexOf(upperPattern,position0))!=-1){for(inti=position0;i4、ition1;++i)chars[count++]=original[i];for(inti=0;i5、);}测试staticvoidMain(string[]args){stringsegment="AaBbCc";stringsource;stringpattern="AbC";stringdestination="Some";stringresult="";constlongcount=1000;StringBuilderpressure=newStringBuilder();HiPerfTimertime;for(inti=0;i6、ssure.ToString();GC.Collect();//regexptime=newHiPerfTimer();time.Start();for(inti=0;i7、;time.Start();for(inti=0;i8、Replace(source,pattern,destination,1,-1,StringCompareMethod.Text);}time.Stop();Console.WriteLine("vbReplace="+time.Duration+
4、ition1;++i)chars[count++]=original[i];for(inti=0;i5、);}测试staticvoidMain(string[]args){stringsegment="AaBbCc";stringsource;stringpattern="AbC";stringdestination="Some";stringresult="";constlongcount=1000;StringBuilderpressure=newStringBuilder();HiPerfTimertime;for(inti=0;i6、ssure.ToString();GC.Collect();//regexptime=newHiPerfTimer();time.Start();for(inti=0;i7、;time.Start();for(inti=0;i8、Replace(source,pattern,destination,1,-1,StringCompareMethod.Text);}time.Stop();Console.WriteLine("vbReplace="+time.Duration+
5、);}测试staticvoidMain(string[]args){stringsegment="AaBbCc";stringsource;stringpattern="AbC";stringdestination="Some";stringresult="";constlongcount=1000;StringBuilderpressure=newStringBuilder();HiPerfTimertime;for(inti=0;i6、ssure.ToString();GC.Collect();//regexptime=newHiPerfTimer();time.Start();for(inti=0;i7、;time.Start();for(inti=0;i8、Replace(source,pattern,destination,1,-1,StringCompareMethod.Text);}time.Stop();Console.WriteLine("vbReplace="+time.Duration+
6、ssure.ToString();GC.Collect();//regexptime=newHiPerfTimer();time.Start();for(inti=0;i7、;time.Start();for(inti=0;i8、Replace(source,pattern,destination,1,-1,StringCompareMethod.Text);}time.Stop();Console.WriteLine("vbReplace="+time.Duration+
7、;time.Start();for(inti=0;i8、Replace(source,pattern,destination,1,-1,StringCompareMethod.Text);}time.Stop();Console.WriteLine("vbReplace="+time.Duration+
8、Replace(source,pattern,destination,1,-1,StringCompareMethod.Text);}time.Stop();Console.WriteLine("vbReplace="+time.Duration+
此文档下载收益归作者所有