Clever string replacement in collection

  • Thread starter Thread starter Sjaakie
  • Start date Start date
S

Sjaakie

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);

}

}
 
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
 
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.
 
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:
 
Back
Top