String.Replace( ) vs. StringBuilder.Replace( )

P

Peter Row

Hi,

I know this has been asked before, but reading the threads it is still not
entirely clear.
Deciding which .Replace( ) to use when.

Typically if I create a string in a loop I always use a StringBuilder. At
present I am
porting a VB6 webclass app to VB.NET and therefore I am trying to make it as
efficent as possible via the new functionality of VB.NET.

At present I have (in various places):
variableX = variableX.Replace("a string", "another string")

In some cases the above variableX will have 4+ replaces virutally one after
the
other with 1 or 2 of the replacement strings having length > 100.

This seems like the perfect candidate for StringBuilder.Replace instead of
String.Replace
what does anybody think?

Regards,
Pete
 
P

Peter Row

Hi,

The link provided gives a lot of info but leaves out some important info
hence the original question.

If I have a stringbuilder and lets say it has length = 100, I also have
strHeaderStuff with length = 200.
For the sake of argument lets also say that .IndexOf("<head>") is 25 and I
say:

MyStrBldr.Replace("<head>", strHeaderStuff)

The stringbuilder object is increasing it's size and inserting at a position
it has to find first.
So I still need to be convinced that doing it this way will be faster than
doing:

SimpleString = SimpleString.Replace("<head>", strHeaderStuff)

I don't wanna start a massive flaming debate about the rights and wrongs of
anything but...
wouldn't it of made more sense for MS to implement the String type to do the
same as a
StringBuilder under the covers and hence not have StringBuilder?

i.e
Dim str As String
Str = "Hello"
Str &= " World"

Is roughly the same as saying:
Dim sbr As New StringBuilder
sbr.Append("Hello")
sbr.Append(" World")

So if the stringbuilder is so much more efficent why not simply implement
the String that way
in the first place?

Regards,
Peter
 
C

Cor

Hi Peter,

Only an answer on your question,

I asume that making a string as dim mystring as String = "Peter" with the
stringbuilder is a little bit overdone.

But nobody is keeping you from doing it with stringbuilder, that is what I
find so nice from VB.net, you can make your own decissions how you do it.

When that is gone you can take a tool as Access.

But just my thought,


Cor
 
P

Peter Row

Hi,

Eh? I don't get what you are saying?

If I have a SBuilder with length 100 and I replace "<head>" (length 6) with
a string
that is 200 in length then the Sbuilder has no choice but to resize surely;
because
you are replacing 6 out of 100 with 200 hence the length after the replace
would be
294 which is 194 larger than before the operation??

Therefore since it is resizing it then you'd assume there is some overhead
to that.
So how does that overhead compare to doing a :
Str = Str.Replace("<head>", str2)

Regards,
Peter
 
P

Peter Row

Hi,

The point I was making was that since when dealing with Strings that are
appended
and replaced over and over the stringbuilder is more efficent then why
bother
making the distinction.

All it does is give you a pointless decision which in some cases is very
close.
It boils down to this:
Do you want your app to run fast/efficently or run okay/more-or-less
efficent?

Well I would always choose fast/efficently as I'm sure most people would,
therefore
why complicate the issue by having String and StringBuilder? Why not just
have
String which works like StringBuilder under the covers?

Finally don't be so digusting!!! Mentioning MS Access, urrgghh!

Regards,
Peter
 
J

Jay B. Harlow [MVP - Outlook]

Peter,
Remember that the StringBuilder over allocates its buffer, a string only
ever holds how ever many characters are in it!

The StringBuilder.Capacity property is how many characters the buffer will
hold, while StringBuilder.Length is how many characters are in use. Normally
you should set StringBuilder.Capacity to a value larger then the expected
resultant string. Other wise as Ken stated the StringBuilder will need to
reallocate its buffer. When the StringBuilder reallocates its buffer, it
doubles it in size, which means after a couple reallocates it is probably
significantly larger then it needs to be, by default capacity starts at 16.
By setting the Capacity value when you start (in the constructor for
example) you save the reallocations of the StringBuilder's buffer. You can
use StringBuilder.MaxCapacity to limit to maximum capacity that a
StringBuilder can be expanded to.

For example, I am creating a new StringBuilder that is initialized to my
template "<head><body><tail>", however I am setting the capacity of that
StringBuilder to 1024, which is clearly larger then my three replacement
values. When I use sb.Replace, because the buffer capacity is larger then
the result, the buffer is not reallocated...

Dim sb As New StringBuilder("<head><body><tail>", 1024)
Dim head As New String("h"c, 200)
Dim body As New String("b"c, 200)
Dim tail As New String("t"c, 200)

sb.Replace("<head>", head)
sb.Replace("<body>", body)
sb.Replace("<tail>", tail)

Hope this helps
Jay
 
P

Peter Row

Hi,

At last someone that has something worthwhile to say, thanks!

So basically you have to take an educated guess based on your situation and
set the capacity.
I will have to do that as a phase 2 optimisation on my porting from VB6
project as I haven't
been setting the capacity when I use one.

Regards,
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top