473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

stringbuilder vs string

Hi

What are the advantages of a stringbuilder compared to a string?

Thanks

Regards
Nov 20 '05 #1
4 8731
A string object is immutable (once it has been created, it cannot be
changed). Contrary to popular belief,

Dim szTest as String

szTest = "Test1"

' This causes szTest to be assigned to a new object rather than "123"
getting bolted onto the back of it
szTest = szTest & "123"
Because a new object is created with every assignment, there is a slight
overhead when doing this repeatedly.

The string builder class provides a more efficient way to repeatedly add
bits on to a string. You provide it with an initial capacity (it grows
automatically, but will degrade performance if it grows too often). You can
add more bits of strings to it without the overhead of creating a new
object.

When you are finished building the string, use the ToSting method of the
StringBuilder to turn it into an ordinary string.

The performance difference will be too small to measure if you're only doing
a few concatenations, but if you're in a loop, building a big string (e.g.
HTML code for a web page) then StringBuilder has a definite advantage.

Hope this helps,

Trev.


"John" <jo**@nospam.infovis.co.uk> wrote in message
news:#3**************@TK2MSFTNGP12.phx.gbl...
Hi

What are the advantages of a stringbuilder compared to a string?

Thanks

Regards

Nov 20 '05 #2

StringBuilder is much faster when you do string operations. String takes
less space in memory.

String type is immutable. There is no way to modify an olready created
string object. When you, for example, appending a character or another
string to a string, a new combined string is actually created every time.
StringBuilder is mutable and works much faster when you need to modify it's
content.

Vladimir [VB.Net team]
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 20 '05 #3
Cor
Codemonkey,
The performance difference will be too small to measure if you're only doing a few concatenations, but if you're in a loop, building a big string (e.g.
HTML code for a web page) then StringBuilder has a definite advantage.


I did believe that too, I did test it this week with a very small program,
and the performance difference is really unbelievable.
(Not of course for one occurrence in a program, but when it is repeated like
in a normal program, I now will fast go to stringbuilder).
Cor
Nov 20 '05 #4
"John" <jo**@nospam.infovis.co.uk> scripsit:
What are the advantages of a stringbuilder compared to a string?


Strings are immutable in .NET, that means that every time you assign a
new value to the string, a new string will be created and the old object
will be deleted. If you compose a long string, for example in a loop,
by concatenating it with the '&' operator, this may be very costly.

In this situation you should better use a 'StringBuilder'. The
StringBuilder reserves some space and allows modifications of its text.
You can call its 'ToString' method to get the string represented by the
StringBuilder after finishing composing the text.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Alvin Bruney | last post by:
On the advice of a user, I've timed stringbuilder v string. Here are the results. Here are the numbers: Total # queries 3747 Time in Milliseconds StringBuilder: String...
4
by: J. Oliver | last post by:
I am attempting to copy a portion of a string into another string. Say I have "abcdef" in string 'a' and want to copy only "bcd" into a new string 'b'. The following function came to mind: ...
18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
9
by: ad | last post by:
I have a string like "1,2,2,3,3,3,4" I want to trim off the duplicate part, and make it to "1,2,3,4" How can I do?
13
by: Matt | last post by:
10 len space designated strings grows when i do sb.insert. is there way to stop string growing. It becomes 14 space len on test.file I like to be able insert 3rd position but length should stay...
2
by: Justin | last post by:
I have a string built using the stringbuilder class and I need to insert a break at the end of the string, how do I do that? Thanks, Justin.
8
by: Arjan | last post by:
Hello, I have been looking for a couple of days now, but I can't find anything about how to deal with StringBuilder and releasing the memory used by it. When I use stringbuilder, the memory...
2
by: Fred | last post by:
Hello, I am having a problem with a multiline string that I create from a byte array (xml file). I am using StringBuilder and Ecoding the string to ASCII (code below). / Upload the...
3
by: zubair1 | last post by:
Hello, Could some body please tell me how i can change the fonts style of a stringbuilder string at runtime. Example: Function myFun () as string Dim MyOutput As New...
1
Paul Johnson
by: Paul Johnson | last post by:
Hi, I have a bit of a problem with a stringbuilder and converting it so that I can use it as a stored proc The original code goes like this The initial part of the stringbuilder is simple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.