解析.NET中字符串替換的五種方法_.Net教程
推薦:.NET教程之--.NET動(dòng)態(tài)創(chuàng)建類的實(shí)例解析看了網(wǎng)上很多關(guān)于DotNet動(dòng)態(tài)創(chuàng)建類的實(shí)例的文章,我這里想總結(jié)一下,其實(shí)方法很簡(jiǎn)單,就是用“Activator.CreateInstance”。但是這個(gè)方法需要待創(chuàng)建的類的Type作為參數(shù),為了獲
1:使用String.Replace函數(shù)替換,但不支持大小寫。
2:正則System.Text.Regex替換,用RegExpOption修改是否支持大小寫。
3:在小數(shù)據(jù)的情況下,使用String.SubString和 可以實(shí)現(xiàn)間接替換。
4:導(dǎo)入MicrosoftVisualBasicRunTime(Microsoft.VisualBasic.DLL)使用Strings.Replace速度很快。
5:參照反射Reflector.FileDisassembler配合Strings.SplitandStrings.Join等實(shí)現(xiàn),速度同5。
一下介紹一種算法,類似KMP算法。有興趣的參照研究下。
| 以下為引用的內(nèi)容: privatestaticstringReplaceEx(stringoriginal, stringpattern,stringreplacement) { intcount,position0,position1; count=position0=position1=0; stringupperString=original.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;i<position1; i) chars[count ]=original[i]; for(inti=0;i<replacement.Length; i) chars[count ]=replacement[i]; position0=position1 pattern.Length; } if(position0==0)returnoriginal; for(inti=position0;i<original.Length; i) chars[count ]=original[i]; returnnewstring(chars,0,count); } |
測(cè)試
| 以下為引用的內(nèi)容: staticvoidMain(string[]args) { stringsegment="AaBbCc"; stringsource; stringpattern="AbC"; stringdestination="Some"; stringresult=""; constlongcount=1000; StringBuilderpressure=newStringBuilder(); HiPerfTimertime; for(inti=0;i<count;i ) { pressure.Append(segment); } source=pressure.ToString(); GC.Collect(); //regexp time=newHiPerfTimer(); time.Start(); for(inti=0;i<count;i ) { result=Regex.Replace(source,pattern, destination,RegexOptions.IgnoreCase); } time.Stop(); Console.WriteLine("regexp =" time.Duration "s"); GC.Collect(); //vb time=newHiPerfTimer(); time.Start(); for(inti=0;i<count;i ) { result=Strings.Replace(source,pattern, destination,1,-1,CompareMethod.Text); } time.Stop(); Console.WriteLine("vb =" time.Duration "s"); GC.Collect(); //vbReplace time=newHiPerfTimer(); time.Start(); for(inti=0;i<count;i ) { result=VBString.Replace(source,pattern, destination,1,-1,StringCompareMethod.Text); } time.Stop(); Console.WriteLine("vbReplace=" time.Duration "s");// result); GC.Collect(); //ReplaceEx time=newHiPerfTimer(); time.Start(); for(inti=0;i<count;i ) { result=Test.ReplaceEx(source,pattern,destination); } time.Stop(); Console.WriteLine("ReplaceEx=" time.Duration "s"); GC.Collect(); //Replace time=newHiPerfTimer(); time.Start(); for(inti=0;i<count;i ) { result=source.Replace(pattern.ToLower(),destination); } time.Stop(); Console.WriteLine("Replace =" time.Duration "s"); GC.Collect(); //sorry,twoslow:( /*//substring time=newHiPerfTimer(); time.Start(); for(inti=0;i<count;i ) { result=StringHelper.ReplaceText(source,pattern, destination,StringHelper.CompareMethods.Text); } time.Stop(); Console.WriteLine("substring=" time.Duration ":"); GC.Collect(); //substringwithstringbuilder time=newHiPerfTimer(); time.Start(); for(inti=0;i<count;i ) { result=StringHelper.ReplaceTextB(source,pattern, destination,StringHelper.CompareMethods.Text); } time.Stop(); Console.WriteLine("substringB=" time.Duration ":"); GC.Collect(); */ Console.ReadLine(); } 1?¢stringsegment="abcaBc"; regexp=3.75481827997692s vb=1.52745502570857s vbReplace=1.46234256029747s ReplaceEx=0.797071415501132s!!!<FONTcolor=gray>Replace=0.178327413120941s</FONT> //ReplaceEx>vbReplace>vb>regexp 2?¢stringsegment="abcaBcabC"; regexp=5.30117431126023s vb=2.46258449048692s vbReplace=2.5018721653171s ReplaceEx=1.00662179131705s!!! <FONTcolor=gray>Replace=0.233760994763301s</FONT> //ReplaceEx>vb>vbReplace>regexp 3?¢stringsegment="abcaBcabCAbc"; regexp=7.00987862982586s vb=3.61050301085753s vbReplace=3.42324876485699s ReplaceEx=1.14969947297771s!!! <FONTcolor=gray>Replace=0.277254511397398s</FONT> //ReplaceEx>vbReplace>vb>regexp 4?¢stringsegment="ABCabcAbCaBcAbcabCABCAbcaBC"; regexp=13.5940090151123s vb=11.6806222578568s vbReplace=11.1757614445411s ReplaceEx=1.70264153684337s!!!(mygod!) <FONTcolor=gray>Replace=0.42236820601501s</FONT> //ReplaceEx>vbReplace>vb>regexp |
查看程序的Block在:
| 以下為引用的內(nèi)容: stringupperString=original.ToUpper(); |
如果需要敏感,就免了這2行。
解釋:先建一個(gè)char[]類型的變量采訪替換后的字符,其大小就是最大可能被替換的字符,例如ABABAB,替換AB成C,其獲取過程就是ABABAB最大可能包括的AB的數(shù)目乘以AB多于C的數(shù)目,
| 以下為引用的內(nèi)容: char[]chars=newchar[original.Length Math.Max(0,inc)]; ,inc不一定大于零。 |
然后循環(huán),用IndexOf索引。賦值。。。判斷,返回。
分享:解析.Net編程接口剖析系列之比較和排序我們知道,與C 相比較,C#以及整個(gè).Net并不支持多繼承,而相應(yīng)的,C#支持了接口,并且支持一個(gè)類型實(shí)現(xiàn)多個(gè)接口。對(duì)于接口的概念,相信大部分讀者已經(jīng)有了很好的了解,而我這里談?wù)剛(gè)人對(duì)
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- asp.net頁面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過程實(shí)現(xiàn)分頁示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- 編程高手 ASP.NET 狀態(tài)管理
- .NET教程之代碼控制頁面部分元素隱藏與顯示
- ASP.NET中實(shí)現(xiàn)模板頁
- 解析VS.net調(diào)試ASP.NET項(xiàng)目出錯(cuò)原因及解決方法
- 解析ASP.NET中基類Page_Load方法后執(zhí)行原因
- Asp.net ajax實(shí)現(xiàn)任務(wù)提示頁面
- GridView顯示服務(wù)器圖片(保存為圖片路徑)
- 在ASP.Net 2.0中實(shí)現(xiàn)多語言界面的方法
- 如何用VB.net實(shí)現(xiàn)sql數(shù)據(jù)庫的備份與恢復(fù)
- 解析ASP.NET中C 和J#的混合應(yīng)用
- 相關(guān)鏈接:
- 教程說明:
.Net教程-解析.NET中字符串替換的五種方法
。