Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

StringBuilder vs String

1 view
Skip to first unread message

Tim Mavers

unread,
May 7, 2002, 11:26:06 AM5/7/02
to
If I understand things correctly, it's recommended to use a StringBuilder
when doing a lot of modifications to the string (like building a string
on-the-fly) or lots of writing. If not, the String class will work fine.
I have been trying to figure out why exactly there is StringBuilder and
String class.

From what I have read, the String class is immutable and completely
thread-safe, while the StringBuilder class is not. Is this true? Does that
mean if I am multi-threading and using StringBuilders I need to make sure I
don't write to it simultaneously?

Is the reason the String class exists is solely to easily accomodate
multi-threaded issues?

Is StringBuilder faster for reads or compares than String?

Thanks,

Nicholas Paldino [.NET/C# MVP]

unread,
May 7, 2002, 11:50:36 AM5/7/02
to
Tim,

See inline.

"Tim Mavers" <web...@hotmail.com> wrote in message
news:3cd7f182$0$29789$45be...@newscene.com...


> If I understand things correctly, it's recommended to use a StringBuilder
> when doing a lot of modifications to the string (like building a string
> on-the-fly) or lots of writing. If not, the String class will work fine.
> I have been trying to figure out why exactly there is StringBuilder and
> String class.
>
> From what I have read, the String class is immutable and completely
> thread-safe, while the StringBuilder class is not. Is this true? Does
that
> mean if I am multi-threading and using StringBuilders I need to make sure
I
> don't write to it simultaneously?

This is correct. The StringBuilder is nothing more than an ArrayList
that holds characters (if you want to think of it that way). You will have
to synchronize access to it just like any other object/collection.

>
> Is the reason the String class exists is solely to easily accomodate
> multi-threaded issues?

The reason that the StringBuffer class exists is because string
operations usually require a memory allocation/reallocation and assignment,
plus the release of the memory of the old string. Performing this
operations repeatedly over time can cause degredation in a system, and the
StringBuffer gets arround it by pre-allocating the memory needed and working
with that. The same concept is used with an array list.

>
> Is StringBuilder faster for reads or compares than String?

I am not sure about this one, but it would be easy to test with a small
sample program.

Hope this helps.


--
- Nicholas Paldino [.NET MVP]
- nicholas...@exisconsulting.com

>
> Thanks,
>
>
>


Joe Feser

unread,
May 7, 2002, 11:53:44 AM5/7/02
to
You can always use the Textwriter and use the Synchronized method to make
sure every thread has a Synchronized version of the information.

You can also download the CLI and see how the StringBuilder works.

--
Joe Feser
Fesersoft
http://www.fesersoft.com/
.Net, XML, XSLT, SQL
Free Code, Articles and Information.


"Tim Mavers" <web...@hotmail.com> wrote in message
news:3cd7f182$0$29789$45be...@newscene.com...

[MVP] Thomas Tomiczek

unread,
May 7, 2002, 11:55:34 AM5/7/02
to
Inline with ***

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsft MVP C#/.NET)

"Tim Mavers" <web...@hotmail.com> wrote in message
news:3cd7f182$0$29789$45be...@newscene.com...

> If I understand things correctly, it's recommended to use a StringBuilder


> when doing a lot of modifications to the string (like building a string
> on-the-fly) or lots of writing. If not, the String class will work fine.
> I have been trying to figure out why exactly there is StringBuilder and
> String class.
>
> From what I have read, the String class is immutable and completely
> thread-safe, while the StringBuilder class is not. Is this true? Does
that
> mean if I am multi-threading and using StringBuilders I need to make sure
I
> don't write to it simultaneously?

Now, seriously - WHAT do you want to make with a string in terms of
maniipulation that is thread safe? Writing a log file - you are wrong with
the string builder. Addint parameters or constructing SQL - you better make
sure the txt ist added in the right order.

The difference is that the string is immutable. As such it is as thread safe
as a rock. It has no changable state, so it is the definition of thread
safe - read only.

On the other hand, MANIPULATING strings with string causes a lot of string
objects to be produced. TONS OF. There is this thing called a garbage
collector. Has to collect them and throw them away.

He uses processing power.

So you are inefficient.

It can make a difference.

> Is the reason the String class exists is solely to easily accomodate
> multi-threaded issues?

No.

> Is StringBuilder faster for reads or compares than String?

No.

Regards

Thomas


-glenn-

unread,
May 7, 2002, 12:46:11 PM5/7/02
to

"[MVP] Thomas Tomiczek" <t.tom...@thona-consulting.com> wrote in message
news:3cd7f900$7...@exchange.thona.root...

> Writing a log file - you are wrong with
> the string builder.

Not necessarily. If you are going to write, say, 2,000 characters per "line"
and it takes a lot to build that line then StringBuilder would indeed be the
thing to use. You have to choose between SB and String based on the
complexity of the string you will be creating.

> Addint parameters or constructing SQL - you better make
> sure the txt ist added in the right order.

Well, of course. Point being?

> On the other hand, MANIPULATING strings with string causes a lot of string
> objects to be produced. TONS OF. There is this thing called a garbage
> collector. Has to collect them and throw them away.

If you are doing a lot of string manipulation, StringBuilder is the thing to
use to avoid these "tons of" strings being created. Sure, the GC will get to
them eventually, but they are expensive to create (in comparison to using a
SB) and expensive to manipulate (again, in comparison to SB).

-glenn-


[MVP] Thomas Tomiczek

unread,
May 7, 2002, 1:05:27 PM5/7/02
to
Inline with ***

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.

(Microsoft MVP C#/.NET)

"-glenn-" <som...@example.com> wrote in message
news:OfWwyae9BHA.1612@tkmsftngp07...


>
> "[MVP] Thomas Tomiczek" <t.tom...@thona-consulting.com> wrote in message
> news:3cd7f900$7...@exchange.thona.root...
> > Writing a log file - you are wrong with
> > the string builder.
>
> Not necessarily. If you are going to write, say, 2,000 characters per
"line"
> and it takes a lot to build that line then StringBuilder would indeed be
the
> thing to use. You have to choose between SB and String based on the
> complexity of the string you will be creating.

Well, NOBODY forces you to write the whiole line at once if you write into a
logfile :-) For some very complex string manipulation I agree, though.


>
> > Addint parameters or constructing SQL - you better make
> > sure the txt ist added in the right order.
>
> Well, of course. Point being?

There was such a point trying to be taken on "thread safety" taht I wanted
to point out that the üprocess of "assemblying a string" is not thread safe
in itself.

> > On the other hand, MANIPULATING strings with string causes a lot of
string
> > objects to be produced. TONS OF. There is this thing called a garbage
> > collector. Has to collect them and throw them away.
>
> If you are doing a lot of string manipulation, StringBuilder is the thing
to
> use to avoid these "tons of" strings being created. Sure, the GC will get
to
> them eventually, but they are expensive to create (in comparison to using
a
> SB) and expensive to manipulate (again, in comparison to SB).

I said exactly this. Your point is?

Thomas

-glenn-

unread,
May 7, 2002, 1:20:23 PM5/7/02
to

"[MVP] Thomas Tomiczek" <t.tom...@thona-consulting.com> wrote in message
news:3cd8...@exchange.thona.root...
**> > > On the other hand, MANIPULATING strings with string causes a lot of
> string
**> > > objects to be produced. TONS OF. There is this thing called a
garbage
**> > > collector. Has to collect them and throw them away.

> >
> > If you are doing a lot of string manipulation, StringBuilder is the
thing
> to
> > use to avoid these "tons of" strings being created. Sure, the GC will
get
> to
> > them eventually, but they are expensive to create (in comparison to
using
> a
> > SB) and expensive to manipulate (again, in comparison to SB).
>
> I said exactly this. Your point is?

Oh, I see what you mean now. When I first read your paragraph above (marked
with **) I thought you were saying to just use String since the GC will take
care of things for you. I see now that your "there is a thing called a GC"
was trying to say that because there is a GC, try to limit what it has to
collect when possible. Sorry, we appear to be saying the same thing in these
two paragraphs.

-glenn-

Cowboy (aka Gregory A. Beamer) [MVP]

unread,
May 7, 2002, 1:43:56 PM5/7/02
to
As the string builder is an array of characters, and is not thread safe, you
do have to be careful accessing from multiple threads. I do not see the real
world scenario where you would write to a StringBuilder from multiple
threads, so I am not sure where the concern is. The String is not mutable,
so multiple alterations create multiple objects, which will degrade
performance if you continue to perform operations; this is why you would use
a stringbuilder instead.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
****
Think outside the box!
****************************************************************************
****


"Tim Mavers" <web...@hotmail.com> wrote in message
news:3cd7f182$0$29789$45be...@newscene.com...

0 new messages