473,887 Members | 2,309 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 (StringObjectCo llection)xc) {

foreach (ReplacementStr ingObject r in
(ReplacementStr ingObjectCollec tion)rc) {

x.Something.Rep lace(r.SomeProp erty, r.SomeValue);

}

}
Jul 7 '06 #1
4 3007
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 (StringObjectCo llection)xc) {

foreach (ReplacementStr ingObject r in
(ReplacementStr ingObjectCollec tion)rc) {

x.Something.Rep lace(r.SomeProp erty, 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 "testgregte st"

replace 1 = "test", "foo" = "foogregfoo "
replace 2 = "foo", "greg" = "greggreggr eg"
replace 3 = "greg", "test" = "testtestte st"

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

"gregoryyou ng"

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*******@yaho o.nospammin.com wrote in message
news:45******** *************** ***********@mic rosoft.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 (StringObjectCo llection)xc) {

foreach (ReplacementStr ingObject r in
(ReplacementSt ringObjectColle ction)rc) {

x.Something.Rep lace(r.SomeProp erty, 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 "testgregte st"

replace 1 = "test", "foo" = "foogregfoo "
replace 2 = "foo", "greg" = "greggreggr eg"
replace 3 = "greg", "test" = "testtestte st"

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

"gregoryyou ng"

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*******@yaho o.nospammin.com wrote in message
news:45******** *************** ***********@mic rosoft.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 (StringObjectCo llection)xc) {

foreach (ReplacementStr ingObject r in
(ReplacementS tringObjectColl ection)rc) {

x.Something.Rep lace(r.SomeProp erty, r.SomeValue);

}

}

Jul 10 '06 #5

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

Similar topics

5
2531
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' ) But to me this seems inefficient since the 'in' operation will search through the whole string, and then the 'replace' will as well.
32
14941
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
34
5736
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 inherits from the collectionbase and uses an arraylist to hold values, but i'm missing the Item(string) property. I suppose I could iterate thru the collection and look for a string match, but I thought there might be an easier way. Thanks,
35
5853
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 collective brainstorming session. Let's discuss some C here, for a change :-)
29
4289
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
3430
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 brackets are not regular expression syntax. Thanks,
94
4830
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 that I found inside of a string. Any ideas?
8
2698
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 and generics is on it - I assume this is why I encounter problems. So this raises a number of questions: - What is the best replacement of List<intin 1.1 (or is the list just in another namespace)?
42
2827
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 there is no characters in the variable it does not enter my IF statement like I would think it should. How do I properly check for a
0
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10434
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7143
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4633
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 we have to send another system
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.