SoFunction
Updated on 2025-04-13

Five ways to replace strings in


staticvoidMain(string[]args)
{
stringsegment="AaBbCc";
stringsource;
stringpattern="AbC";
stringdestination="Some";
stringresult="";

constlongcount=1000;
StringBuilderpressure=newStringBuilder();
HiPerfTimertime;
for(inti=0;i<count;i++)
{
(segment);
}
source=();
();
//regexp
time=newHiPerfTimer();
();
for(inti=0;i<count;i++)
{
result=(source,pattern,
 destination,);
}
();
("regexp="++"s");
();
//vb
time=newHiPerfTimer();
();
for(inti=0;i<count;i++)
{
result=(source,pattern,
 destination,1,-1,);
}
();
("vb="++"s");
();
//vbReplace
time=newHiPerfTimer();
();
for(inti=0;i<count;i++)
{
result=(source,pattern,
 destination,1,-1,);
}
();
("vbReplace="++"s");//+result);
();
//ReplaceEx
time=newHiPerfTimer();
();
for(inti=0;i<count;i++)
{
result=(source,pattern,destination);
}
();
("ReplaceEx="++"s");
();
//Replace
time=newHiPerfTimer();
();
for(inti=0;i<count;i++)
{
result=((),destination);
}
();
("Replace ="++"s");
();
//sorry,twoslow:(
/*//substring
time=newHiPerfTimer();
();
for(inti=0;i<count;i++)
{
result=(source,pattern,
 destination,);
}
();
("substring="++":");
();
//substringwithstringbuilder
time=newHiPerfTimer();
();
for(inti=0;i<count;i++)
{
result=(source,pattern,
destination,);
}
();
("substringB="++":");
();
*/
();
}
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