Performance Regex Replace vs StringBuilder Replace

  • Thread starter Jay B. Harlow [MVP - Outlook]
  • Start date
J

Jay B. Harlow [MVP - Outlook]

Paul,
I suspect it highly depends on what you are replacing.

Regex.Replace is intended for replacing a pattern, as in a regular
expression pattern.

While StringBuilder.Replace is meant for replacing a specific string or char
(not a pattern).

Remember there is also String.Replace that is useful to replace a specific
string or char.

Generally I find its better to write "correct" programs first, then change
the code for performance later, only when that routine has proven to have
performance problems via profiling!

By "correct" I mean use Regex.Replace if you are replacing a regular
expression, other wise use StringBuilder.Replace or String.Replace based on
needing a StringBuilder or not. I would consider the StringBuilder if I had
multiple replacements on a single String. For a single replacement I would
use String.Replace.

Hope this helps
Jay
 
P

paul reed

Hi,

What is more efficient, using the regex replace capability or the
stringbuilder replace capability when replacing sting chunks within a larger
string?

Regards,

Paul
 
P

Paul Reed

Cor,

Thanks. I am surprised that String.Replace came out so well as I thought
any of the String manipulation methods always creates a new String while
the StringBuilder doesn't.

Also, I agree with the first gentlemens post...it really depends on what
you are replacing that will drive you towards the correct solution.

Paul Reed
www.jacksonreed.com
 
P

Paul Reed

Jay,

Will said. I do a lot of OO training and I always tell my students that
"early optimization is the root of all evil" not my quote but one that
is so true. In my case, I am dynamically building a SQL statement that
has been sent to me with logical tokens embedded that represent desired
field columns, tables, etc.. I have to parse that then look up those
tokens in a database and replace them with the real thing (i.e., this
<<Recipient.FirstName>> in the select statement needs to get converted
to "Person.FirstName as FName" in the SQL Statement. So, I use Regex to
find the patterns <<XXXX>> then I (am) going use the stringbuilder
replace to replace the string.

Paul Reed
www.jacksonreed.com
 
J

Jay B. Harlow [MVP - Outlook]

Paul,
Does Recipient always map to Person & Fname? Or does it vary?

In other words after you find the <<XXXX>> pattern do you take the XXXX part
of the match & decide what needs to be replaced?

It sounds like you are: I found this match, now based on what I found
replace this value.

It sounds like you may actually want to use the nonexistent Match.Replace
;-)

As Cor suggested in this case I would weigh carefully choosing
StringBuilder.Replace over String.Replace... Especially if you are doing a
Replace, then passing this new string back to RegEx to find another match.

Hope this helps
Jay
 
P

Paul Reed

Jay,

You are exactly right...that is what I am doing. Once I match on that
pattern...I then have to access a repository to figure out what to turn
it into.

I am going to use the StringBuilder.Replace method.

Thanks for all of your support on these message boards...we couldn't
survive without it.

Regards,

Paul Reed
www.jacksonreed.com
 
J

Jay B. Harlow [MVP - Outlook]

Paul,
Not sure if you are still following this thread. I just saw this post in the
csharp newsgroup.

You can use a MatchEvaluator (a delegate) to call a routine for each match
you find, then you can simply call Regex.Replace with your pattern & the
AddressOf your routine. The routine will take the inner stuff found & return
the new value.

Here is the C# code posted in the C# thread:

Regex.Replace("ABCD", "(AB|CD)", new MatchEvaluator(this.Match_AB_CD))

private string Match_AB_CD(Match m) {
switch(m.Groups[0].Value) {
case "AB": return "12"; break;
case "CD": return "34"; break;
}
}

Post if you need VB.NET code.

Hope this hleps
Jay
 
J

Jeffrey Tan[MSFT]

Hi Paul,

Does the community's reply reply make sense to you?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top