StringBuilder

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

API question...

Why doesn't StringBuilder have IndexOf and other similar methods like
String? I can't think of a good reason off the top of my head.

Easy to write helper functions to do the same thing, but sure would be
helpful in certain cases.

KH
 
API question...

Why doesn't StringBuilder have IndexOf and other similar methods like
String? I can't think of a good reason off the top of my head.

Easy to write helper functions to do the same thing, but sure would be
helpful in certain cases.

Because it would duplicate functionality of string. StringBuilder is just
that - it *builds* strings. You can easily access the current contents of
the StringBuilder, so if you want you can just use:

oStringBuilder.ToString().IndexOf(...)

-mdb
 
No need for the attitude there hombre, I'm well aware of the difference
between the two classes.

Calling ToString() on StringBuilder (or any object for that matter) creates
a new string object, which can start hurting performance if you do it enough
times.

I still can't think of a reason it doesn't have those methods. Could it and
they just aren't implemented, or is there a reason it *can't* have them.
maybe something with thread safety? I doubt that one though.
 
What attitude are you referring to? The fact that I put *'s around one
of my words for emphasis? You did the exact same thing.... I answered
your question pretty directly, I think.

Anyway, there's no reason I can think of that it couldn't have those
methods - it just doesn't. Although even if it did, internally it would
probably just do the same thing (call ToString.IndexOf(...)).

Personally I think everyone harping on with "Don't use strings - use
StringBuilder" is a bit over-zealous in many cases. Yes there are
performance and memory implications. But more often than not, the
people that are writing the programs are writing a tic-tac-toe game or
something similar.

In fact, looking at the code for StringBuilder.ToString(), there are
some thread safety issues that it deals with. However, it would also
appear that a fair percentage of the time, you're just going to get back
the string that is internal to the StringBuilder anyway after it has
been fixed up.

Disclaimer: I haven't analyzed the class in full force so I might be off
on the details here.

public override string ToString()
{
string text1 = this.m_StringValue;
int num1 = this.m_currentThread;
if ((num1 != 0) && (num1 != StringBuilder.InternalGetCurrentThread
()))
{
return string.InternalCopy(text1);
}
if ((2 * text1.Length) < text1.ArrayLength)
{
return string.InternalCopy(text1);
}
text1.ClearPostNullChar();
this.m_currentThread = 0;
return text1;
}

-mdb
 
Hi,


KH said:
No need for the attitude there hombre, I'm well aware of the difference
between the two classes.

I see no attitude there, just a straight answer to your question.
Calling ToString() on StringBuilder (or any object for that matter)
creates
a new string object, which can start hurting performance if you do it
enough
times.

True, not idea why those utils method were not implemented in
StringBuilder.; I have never need them though.
 
Hi,

Michael Bray said:
What attitude are you referring to? The fact that I put *'s around one
of my words for emphasis? You did the exact same thing.... I answered
your question pretty directly, I think.
True

Anyway, there's no reason I can think of that it couldn't have those
methods - it just doesn't. Although even if it did, internally it would
probably just do the same thing (call ToString.IndexOf(...)).

It depends of how you keep track of the different pieces of string, not sure
how StringBuilder is implemented. As I said before I have never come around
the need to use that method in code.
Personally I think everyone harping on with "Don't use strings - use
StringBuilder" is a bit over-zealous in many cases. Yes there are
performance and memory implications. But more often than not, the
people that are writing the programs are writing a tic-tac-toe game or
something similar.

There is an article, I think in MSDN magazine where its shown that for just
a few concatenations it's the same using either method.
 
Calling ToString() on StringBuilder (or any object for that matter)
creates
a new string object, which can start hurting performance if you do it
enough
times.

How did you arrive at that conclusion? Care to point out supporting
documentation?

--
Warm Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley 2006
Blog: http://msmvps.com/blogs/Alvin/
 
I was surprised to find out that the StringBuilder.ToString method does not
create a new instance of a string. However, if you use the StringBuilder
after calling this method, it does, in order to preserve the immutability of
the string it returned from the first call.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

Alvin Bruney - ASP.NET MVP said:
Calling ToString() on StringBuilder (or any object for that matter)
creates
a new string object, which can start hurting performance if you do it
enough
times.

How did you arrive at that conclusion? Care to point out supporting
documentation?

--
Warm Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley 2006
Blog: http://msmvps.com/blogs/Alvin/
-------------------------------------------------------



"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:#[email protected]...
Hi,




I see no attitude there, just a straight answer to your question.


True, not idea why those utils method were not implemented in
StringBuilder.; I have never need them though.
 

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

Back
Top