By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,826 Members | 815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,826 IT Pros & Developers. It's quick & easy.

Fastest String Replacement Method?

P: n/a
Hello,

What is the fastest/most efficient way of doing string replacement in
csharp/.net?

An example would be:

"Hello, my name is {FirstName}, and I live in the town of {City} with
my wife {WifeName} and my {NumKids} children."

I have this working with regular expressions, but I'm wondering if
that's the best/fastest method. Would writing this in another
language (say, C++) improve the performance?

Thanks for any insight.
Phil

Feb 26 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Are regular expressions significanly faster than the String.Replace()
method?

Have you tried using some unsafe code in your class?

Nothing's going to be faster than good ole C !!

-tomas

On 26 Feb 2007 07:47:45 -0800, "Phil Sandler" <ps********@hotmail.com>
wrote:
>Hello,

What is the fastest/most efficient way of doing string replacement in
csharp/.net?

An example would be:

"Hello, my name is {FirstName}, and I live in the town of {City} with
my wife {WifeName} and my {NumKids} children."

I have this working with regular expressions, but I'm wondering if
that's the best/fastest method. Would writing this in another
language (say, C++) improve the performance?

Thanks for any insight.
Phil
Feb 26 '07 #2

P: n/a


"Phil Sandler" <ps********@hotmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
Hello,

What is the fastest/most efficient way of doing string replacement in
csharp/.net?

An example would be:

"Hello, my name is {FirstName}, and I live in the town of {City} with
my wife {WifeName} and my {NumKids} children."

I have this working with regular expressions, but I'm wondering if
that's the best/fastest method.
Is is simple? Does it perform well enough for your application?
>Would writing this in another language (say, C++) improve the performance?
Probably, but it would be unsafe code. And there are probably faster,
though more complicated ways to do this safely in managed code. For
instance you could read through the template word-by-word and build the
output string as you go with a StringBuilder, replacing the codes as you
find them.

David
Feb 26 '07 #3

P: n/a
Phil Sandler wrote:
Hello,

What is the fastest/most efficient way of doing string replacement in
csharp/.net?

An example would be:

"Hello, my name is {FirstName}, and I live in the town of {City} with
my wife {WifeName} and my {NumKids} children."

I have this working with regular expressions, but I'm wondering if
that's the best/fastest method. Would writing this in another
language (say, C++) improve the performance?

Thanks for any insight.
Phil
If you do the replacement using a delegate so that you only do one
replacement operation, then I think that a regular expression is the
best method.

I am sure that you can find a method that performs better under certain
conditions, but I am also pretty sure that no method that is nearly as
simple as a Reex replace scales nearly as well.

If you for example do a string Replace for each keyword you will get
better results for a short string and a low number of replacements, but
if the number of replacements is doubled, so is the execution time,
which is not the case with a Regex Replace.

Of course you can write an optimised version of what the Regex Replace
does in this specific case, but that is a lot of work for quite a small
improvement.

--
Göran Andersson
_____
http://www.guffa.com
Feb 26 '07 #4

P: n/a

"Phil Sandler" <ps********@hotmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
Hello,

What is the fastest/most efficient way of doing string replacement in
csharp/.net?

An example would be:

"Hello, my name is {FirstName}, and I live in the town of {City} with
my wife {WifeName} and my {NumKids} children."

I have this working with regular expressions, but I'm wondering if
that's the best/fastest method. Would writing this in another
language (say, C++) improve the performance?
Depends on whether a particular template is reused with different data... if
so, it makes a lot of sense to parse it only once, then the parsing speed
becomes less important than the final encoding. Something like:

array of literals: { "Hello, my name is ", ", and I live in the town of ",
.... }
array of field indexes, negative if reference to literal, positive if
reference to database field

will take moderate additional processing to set up, but be very fast to
execute across a dataset.
>
Thanks for any insight.
Phil

Feb 26 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.