在.NET中字符串替换的五种⽅法1:使⽤String.Replace函数替换,但不⽀持⼤⼩写。
2:正则System.Text.Regex替换,⽤RegExpOption修改是否⽀持⼤⼩写。
3:在⼩数据的情况下,使⽤String.SubString和+可以实现间接替换。
4:导⼊MicrosoftVisualBasicRunTime(Microsoft.VisualBasic.DLL)使⽤Strings.Replace速度很快。5:参照反射Reflector.FileDisassembler配合Strings.SplitandStrings.Join等实现,速度同5。
⼀下介绍⼀种算法,类似KMP算法。有兴趣的参照研究下。
以下为引⽤的内容:
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);
}
测试
以下为引⽤的内容:
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++)
for(inti=0;i<count;i++)
{
result=Regex.Replace(source,pattern,
字符串replace函数 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++)
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在:
以下为引⽤的内容:
stringupperString=original.ToUpper();
stringupperPattern=pattern.ToUpper();
如果需要敏感,就免了这2⾏。
解释:先建⼀个char[]类型的变量采访替换后的字符,其⼤⼩就是最⼤可能被替换的字符,例如ABABAB,替换AB成C,其获取过程就是ABABAB最⼤可能包括的AB的数⽬乘以AB多于C的数⽬,
以下为引⽤的内容:
char[]chars=newchar[original.Length+Math.Max(0,inc)];
,inc不⼀定⼤于零。
然后循环,⽤IndexOf索引。赋值。。。判断,返回。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论