• Home
  • Blog
  • Demos
  • About
  • ------------------
  • Articles
  • Csharp(59)
    • controls (4)
    • methods (5)
    • tutorials (50)
  • Html(1)
    • tutorials (1)
  • Java(10)
    • mobile (10)
  • Javascript(5)
    • tutorials (5)
  • Linux(3)
    • tutorial (3)
  • Math(10)
    • tutorials (10)
  • Php(15)
    • functions (1)
    • mysql (6)
    • tutorials (8)
  • Sql(23)
    • tutorials (23)
  • Vba(1)
    • tutorials (1)

The difference between String and StringBuilder

Written: 2010-02-05 06:55:54
Mood: Nulled
Subject: programming
Right!

Today it stood clear to me why it is important to use StringBuilder instead of String when concatenating several strings into one, for example:
String s = "Hello" + "World" + "!";


The reason for this is that String is an array of characters that is immutable. That means that the String object has a fixed size from the start and has to resize every time the limit is broken.
To explain this further, if you have an array of integers set with the size 10 and you want to insert another integer after filling it up you have to create a new integer array and transfer all the old values to the new one, like this:

FAULTY:


int[] myInts = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
myInts[10] = 11; //This will cause a runtime error


CORRECT:


int[] myInts = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//myInts[10] = 11;

int[] myNewInts = new int[11];
for (int i = 0; i < 10; i++)
{
myNewInts[italic] = myInts[italic];
}
myNewInts[10] = 11;


In the correct way we create a new integer array with a large enough size to hold the new value as well. This is also how the ArrayList works, it starts of with a default size (say 16), and whenever that limit is breached, it resizes (doubles it size) by transfering all the old values to a new array with a larger size.

The String object does the same, and if you add alot of stuff to a String object you will waste alot of time (like in the example below:
String container = "";

int count = 0;
for (int i = 0; i < 10000; i++)
{
container += "Hello World!";
count++;
}


The above code will add the string "Hello World!" to an existing string 10.000 times. This is a pretty lame example, but I will demonstrate how ineffective this method is.

Luckily, there is a class in the .NET framework that lets us bypass this problem; the StringBuilder class.
The real problem is that we don't want to create a new String object in memory every time you modify the string, the StringBuilder does not which makes it a lot more effective to use when working with large strings.

I wrote a small application to compare the time it takes for the app to add "Hello World!" 10.000 times and measure the time it took when using String and when using StringBuilder, here's the outcome:

Visual proof that StringBuilder is better than String (sometimes)

The code for this app looked like this:
using System;
using System.Text;
using System.Windows.Forms;

namespace StringTest
{
public partial class Form1 : Form
{
String insertString = "Hello World!";
int stringAttempts;
int stringBuilderAttempts;

public Form1()
{
InitializeComponent();
stringAttempts = 1;
stringBuilderAttempts = 1;
}

private void btnString_Click(object sender, EventArgs e)
{
String container = "";

DateTime startTime = DateTime.Now;
int count = 0;
for (int i = 0; i < 10000; i++)
{
container += insertString;
count++;
}
DateTime stopTime = DateTime.Now;
TimeSpan elapsedTime = stopTime - startTime;

txtOutput.Text += "String experiment " + stringAttempts.ToString() + Environment.NewLine;
txtOutput.Text += "The phrase \"Hello World!\" was added " + count.ToString() + " times." + Environment.NewLine;
txtOutput.Text += "Elapsed time: " + elapsedTime.ToString() + Environment.NewLine;
txtOutput.Text += "_______________________________________________________" + Environment.NewLine + Environment.NewLine;
stringAttempts++;
}

private void btnStringBuilder_Click(object sender, EventArgs e)
{
StringBuilder container = new StringBuilder();

DateTime startTime = DateTime.Now;
int count = 0;
for (int i = 0; i < 10000; i++)
{
container.Append(insertString);
count++;
}
DateTime stopTime = DateTime.Now;
TimeSpan elapsedTime = stopTime - startTime;

txtOutput.Text += "StringBuilder experiment " + stringBuilderAttempts.ToString() + Environment.NewLine;
txtOutput.Text += "The phrase \"Hello World!\" was added " + count.ToString() + " times." + Environment.NewLine;
txtOutput.Text += "Elapsed time: " + elapsedTime.ToString() + Environment.NewLine;
txtOutput.Text += "_______________________________________________________" + Environment.NewLine + Environment.NewLine;
stringBuilderAttempts++;
}
}
}



The difference is remarkable when you loop like this.

However, String is not evil and is more useful when it comes to smaller amounts of text, but if you are adding text in a loop larger than, say, 4 or 5 times then StringBuilder is better to use.