473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Fastest String Replacement Method?

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
4 5850
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


"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jonathan | last post by:
I am hoping that someone more experienced than myself can point me towards what might be the fastest data lookup method to use for storing ip addresses. My situation is that I will need to maintain...
9
by: Jiho Han | last post by:
Suppose I have an xml fragment like: <mother> <child name="Bob" sex="M"/> <child name="Jane" sex="F"/> ... </mother> If I wanted to replace the <mother> element to <father> element, what is...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
14
by: Mark Broadbent | last post by:
Does anybody know what is (factual please -not just guess) the quickest method to read data from a file? I am not interested in the format of the data (i.e. blocks, bytes, string etc) just that the...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
22
by: SETT Programming Contest | last post by:
The SETT Programming Contest: The fastest set<Timplementation Write the fastest set<Timplementation using only standard C++/C. Ideally it should have the same interface like std::set. At least...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.