Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 21st, 2007, 06:15 AM
Registered User
Join Date: Sep 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default String Mystery

I am confused between String And StringBuilder after reading an amazing article.


I do not understand that the author explains the right or wrong idea.

Can anyone help me and tell that what is the best.

Reply With Quote
  #2 (permalink)  
Old September 21st, 2007, 06:25 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

Well as with all things in programming there are almost no definites but in general if you are building a long string from many parts then using the StringBuilder class is better.
If you've got a particular instance where you can't decide then post it here although performance measuring is hard, you need to know in what circumstances the code is run, web or desktop, multiple users or one at a time, processor count, threading etc.
Also String has many methods that StringBuilder doesn't so you don't always have a choice.


Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old September 21st, 2007, 08:12 AM
Authorized User
Join Date: Nov 2006
Location: Valparaiso, IN, USA.
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post

I thought the article was pretty straight forward. In a nutshell...

If you are building a string from user input use a String because the user won't be able to type fast enough to see a difference.

If you are sifting through a large amount of data, say, scanning for a specific word or character, use StringBuilder to concat the resulting string.

One other thing, StringBuilder will expose the underlying array of characters for editing where String will not. (There is the Chars array but it's read only). So, if you wanted to scan a string and replace certain characters you want StringBuilder.

What you don't know can hurt you!
Reply With Quote
  #4 (permalink)  
Old October 6th, 2007, 04:19 PM
Authorized User
Join Date: Dec 2004
Location: Swindon, Wilts, United Kingdom.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DZukiewicz

I wrote an article about this a while ago:


Its a huge performance benefit too.


Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
for-each looping - mystery comma return Navy1991_1 XSLT 6 June 5th, 2008 01:52 PM
error mystery htummala Oracle 2 February 28th, 2005 02:28 AM
Chapter 12 uploading mystery Graham Long BOOK: Beginning ASP 3.0 2 March 18th, 2004 04:10 PM
MM:DataSet mystery bridon123 Dreamweaver (all versions) 2 January 12th, 2004 10:44 PM

All times are GMT -4. The time now is 06:38 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.