Stringbuilder class

J

Jon Slaughter

If StringBuilder has the ability to Insert and Remove then why not EndsWith
and StartsWith? Hell, why not even something like Substring and Split which
would return strings(or even stringbuilders)

To me this class seems to be the most useless class?

I'm building up a very large string from network data by appending text data
together. Once that is done I want to parse the text. Using StringBuilder
only seems to help in one respect while using a string only in the
other(well, it does both but slow on appending). Surely there is something
that has the best of both worlds.

Do I need to convert the StringBuilder into a string after I do the
appending? (I'm worried about performance impact of this when it seems there
is no real reason for it)

(I just don't see why they couldn't have added more functionality to
StringBuilder)

Thanks,
Jon
 
J

Jon Skeet [C# MVP]

Jon Slaughter said:
If StringBuilder has the ability to Insert and Remove then why not EndsWith
and StartsWith? Hell, why not even something like Substring and Split which
would return strings(or even stringbuilders)

To me this class seems to be the most useless class?

Interesting, as many people find it very useful indeed...
I'm building up a very large string from network data by appending text data
together. Once that is done I want to parse the text. Using StringBuilder
only seems to help in one respect while using a string only in the
other(well, it does both but slow on appending). Surely there is something
that has the best of both worlds.

Just call ToString() and then parse.
Do I need to convert the StringBuilder into a string after I do the
appending? (I'm worried about performance impact of this when it seems there
is no real reason for it)

Calling ToString on a StringBuilder has no performance implications at
all *so long as you don't then append to the StringBuilder again*.
StringBuilder holds a string internally - they're mutable as far as
mscorlib is concerned. ToString() just returns the string and remembers
that it has been made public and therefore can't be changed any more.
(I just don't see why they couldn't have added more functionality to
StringBuilder)

Single responsibility, I guess. StringBuilders are used for *building*
strings, not parsing them.
 

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