473,511 Members | 15,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clever string replacement in collection

I need to replace parts of a string, in a collection deserialized from
an XML file, with values from another collection. Is there another, more
clever/faster/better, method than the loops below?

TIA
foreach (StringObject x in (StringObjectCollection)xc) {

foreach (ReplacementStringObject r in
(ReplacementStringObjectCollection)rc) {

x.Something.Replace(r.SomeProperty, r.SomeValue);

}

}
Jul 7 '06 #1
4 2984
Sjaakie,
Probably not, since you are working with strings.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Sjaakie" wrote:
I need to replace parts of a string, in a collection deserialized from
an XML file, with values from another collection. Is there another, more
clever/faster/better, method than the loops below?

TIA
foreach (StringObject x in (StringObjectCollection)xc) {

foreach (ReplacementStringObject r in
(ReplacementStringObjectCollection)rc) {

x.Something.Replace(r.SomeProperty, r.SomeValue);

}

}
Jul 7 '06 #2
Peter Bromberg [C# MVP] schreef:
Sjaakie,
Probably not, since you are working with strings.
Peter
That makes me curious, what other datatypes make a different approach
possible? And how to replace things then?

Cheers
Jul 7 '06 #3
There are quite a few ways you could make this a bit faster.

The first question I would have is do you mean to support chaining? i.e.

string "testgregtest"

replace 1 = "test", "foo" = "foogregfoo"
replace 2 = "foo", "greg" = "greggreggreg"
replace 3 = "greg", "test" = "testtesttest"

or do you really want a single replace per string?

If you are doing lots of these replaces, a stringbuilder will probably be a
bit faster http://msdn2.microsoft.com/en-us/library/3h5afh4w.aspx

If you are doing a whole lot of these replaces (i.e. millions) and these are
big strings you may want to look at things like building indexes to your
string data as opposed to using a simple string.replace. The time spent
building the index in your data would quickly be faster than the linear
searches. As an example ...

"gregoryyoung"

ends up with an index of (for simplicity an alphabet based index 0-25)

My first pattern to test is "gary"
it starts with a g (letter 7 so array posiiton 6). When I go there I get the
indexes 0,3,11 I test to see if I have 1 or 4 in a (I ignore 11 since my
string can't possible fit) in this case I performed 2 array lookups and 2
comparisons. dealign with the string directly I would have had 9
comparisons.

The real benefit is when I get a pattern like "foo", I check my index and
there is no f .. so I just immediately fail out (as opposed to 9
comparisons). These numbers get much bigger with bigger strings. I would
however only recommend this method if you really are doing alot of
comparisons.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

Also .. you could feasably do all of the replaces in a single pass.
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:45**********************************@microsof t.com...
Sjaakie,
Probably not, since you are working with strings.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Sjaakie" wrote:
>I need to replace parts of a string, in a collection deserialized from
an XML file, with values from another collection. Is there another, more
clever/faster/better, method than the loops below?

TIA
foreach (StringObject x in (StringObjectCollection)xc) {

foreach (ReplacementStringObject r in
(ReplacementStringObjectCollection)rc) {

x.Something.Replace(r.SomeProperty, r.SomeValue);

}

}

Jul 7 '06 #4
That's an interesting approach, but there are not that many strings I
need to replace. Only about 100 or so. Maybe I can save some cycles by
replacing the strings when data is still in XML. (before I deserialize
the XML-file).

Would Regex be efficient?

Greg Young schreef:
There are quite a few ways you could make this a bit faster.

The first question I would have is do you mean to support chaining? i.e.

string "testgregtest"

replace 1 = "test", "foo" = "foogregfoo"
replace 2 = "foo", "greg" = "greggreggreg"
replace 3 = "greg", "test" = "testtesttest"

or do you really want a single replace per string?

If you are doing lots of these replaces, a stringbuilder will probably be a
bit faster http://msdn2.microsoft.com/en-us/library/3h5afh4w.aspx

If you are doing a whole lot of these replaces (i.e. millions) and these are
big strings you may want to look at things like building indexes to your
string data as opposed to using a simple string.replace. The time spent
building the index in your data would quickly be faster than the linear
searches. As an example ...

"gregoryyoung"

ends up with an index of (for simplicity an alphabet based index 0-25)

My first pattern to test is "gary"
it starts with a g (letter 7 so array posiiton 6). When I go there I get the
indexes 0,3,11 I test to see if I have 1 or 4 in a (I ignore 11 since my
string can't possible fit) in this case I performed 2 array lookups and 2
comparisons. dealign with the string directly I would have had 9
comparisons.

The real benefit is when I get a pattern like "foo", I check my index and
there is no f .. so I just immediately fail out (as opposed to 9
comparisons). These numbers get much bigger with bigger strings. I would
however only recommend this method if you really are doing alot of
comparisons.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

Also .. you could feasably do all of the replaces in a single pass.
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:45**********************************@microsof t.com...
>Sjaakie,
Probably not, since you are working with strings.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Sjaakie" wrote:
>>I need to replace parts of a string, in a collection deserialized from
an XML file, with values from another collection. Is there another, more
clever/faster/better, method than the loops below?

TIA
foreach (StringObject x in (StringObjectCollection)xc) {

foreach (ReplacementStringObject r in
(ReplacementStringObjectCollection)rc) {

x.Something.Replace(r.SomeProperty, r.SomeValue);

}

}

Jul 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2508
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in text: text = text.replace( searchTerm, 'other' ) ...
32
14762
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...
34
5637
by: Craig Buchanan | last post by:
Which vb.net object is the best match for the vb6 collection class? Specifically, I would like to be able to access the Item property with an index or a key string. I wrote my own class that...
35
5791
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a...
29
4226
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
3356
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...
94
4642
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
8
2675
by: per9000 | last post by:
Hi, I wanted to test to compile an application I build for .NET 2.0 in with the 1.1 C# compiler. I encountered difficulties since I had a List<myClass>. I found a list of what is new in .NET 2.0...
42
2764
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
0
7245
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
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7085
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...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.