Current location: Hot Scripts Forums » Programming Languages » Windows .NET Programming » When exactly should a StringBuilder be used?


When exactly should a StringBuilder be used?

Reply
  #1 (permalink)  
Old 11-09-03, 02:37 PM
gicio gicio is offline
Newbie Coder
 
Join Date: Sep 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Question When exactly should a StringBuilder be used?

Hi!

I’m very interesting in when to use exactly the StringBuilder?

For example for something like this?:

String strTest1 = “This”;
String strTest2 = “Test”;
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append(“is a ”). Append(stbTest);


can someone provide some sample codes when to use a StringBuilder?

And can someone tell me his experience about the performance of a StringBuilder?


Regards,


gicio
Reply With Quote
  #2 (permalink)  
Old 11-22-03, 02:00 AM
psoutham psoutham is offline
New Member
 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Read this http://blogs.gotdotnet.com/ricom/Per...e-0a72e5c1548a

Quote:
Originally Posted by gicio
Hi!

I’m very interesting in when to use exactly the StringBuilder?

For example for something like this?:

String strTest1 = “This”;
String strTest2 = “Test”;
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append(“is a ”). Append(stbTest);


can someone provide some sample codes when to use a StringBuilder?

And can someone tell me his experience about the performance of a StringBuilder?


Regards,


gicio
Reply With Quote
  #3 (permalink)  
Old 04-22-05, 01:49 PM
ben_ ben_ is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
StringBuilders should be used when you want to create and manipulate a large string a lot.

Example:
StringBuilder.sb = new StringBuilder();
sb.Append("Pretend this is a really really really really long string");
sb.Insert(9, "that"); // adds 'that' to the string at char index 9
Response.Write(sb); // or Console.WriteLine(sb);

In terms of the performance difference, it can add up to quite a bit with arge strings. Each method in a regular string (like, Replace for instance) creates a new string in memory. Eg:
string z = "boo";
z = z.Replace("b", "m");
z = z.ToUpper();

This is actually 3 strings stored in the memory, compared to a StringBuilder,
StringBuilder z = new StringBuilder("boo");
z = z.Replace("b", "m");

This is 1 string in the memory.

It's not an issue with a smaller string like here, but if the string was for instance a thousand word block of text and you wanted to replace some words in it then it can add up to a fair amount of overhead.

Last edited by ben_; 04-22-05 at 02:05 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 02:16 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.