Pages

Men

rh

6/16/2012

String Vs Sringbuilder in C#



String Builder

The following table lists the methods you can use to modify the contents of a StringBuilder.

Method name
Use
Appends information to the end of the currentStringBuilder.
Replaces a format specifier passed in a string with formatted text.
Inserts a string or object into the specified index of the current StringBuilder.
Removes a specified number of characters from the current StringBuilder.
Replaces a specified character at a specified index.

Append

The Append method can be used to add text or a string representation of an object to the end of a string represented by the current StringBuilder. 
Example:
StringBuilder MyStringBuilder = new StringBuilder ("Hello World!");
MyStringBuilder. Append (" What a beautiful day.");
Console.WriteLine (MyStringBuilder);

Append Format

The Append Format method adds text to the end of the StringBuilder
Exmple:
int MyInt = 25; 
StringBuilder MyStringBuilder = new StringBuilder("Your total is ");
MyStringBuilder.AppendFormat ("{0:C} ", MyInt);
Console.WriteLine (MyStringBuilder);

O/P-> Your total is $25.00

Insert

The Insert method adds a string or object to a specified position in the current StringBuilder. The following example uses this method to insert a word into the sixth position of a StringBuilder.
Exmple:
 
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder. Insert(6,"Beautiful ");
Console.WriteLine (MyStringBuilder);

Remove

You can use the Remove method to remove a specified number of characters from the current StringBuilder. beginning at a specified zero-based index.
Exmple:
 
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder. Remove(5,7);
Console.WriteLine(MyStringBuilder);
 
O/P->This example displays Hello to the console.

Replace

The Replace method can be used to replace characters within the StringBuilder object with another specified character. 
Exmple:
 
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder. Replace('!', '?');
Console.WriteLine(MyStringBuilder);
This example displays Hello World? to the console.

Setting the Capacity and Length

Although the StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates, you can specify a value for the maximum number of characters that it can hold. This value is called the capacity of the object and should not be confused with the length of the string that the current StringBuilder holds.
 
Exmple:
 
MyStringBuilder.Capacity = 25;
 
StringBuilder MyStringBuilder = new StringBuilder ("Hello World!", 25);  

Additionally, you can use the read/write Capacity property to set the maximum length of your object. The following example uses the Capacity property to define the maximum object length.


Why String? Can't Use StringBuilder Everywhere?
  •  Both String and StringBuilder are classes used to handle strings.
  • StringBuilder can be used where more than four or more string concatenations take place
  • If you try to do some other manipulation (like removing a part from the string, replacing a part in the string, etc.), then it's better not to use StringBuilder at those places. This is because we are anyway creating new strings.
  • We must be careful to guess the size of StringBuilder. If the size which we are going to get is more than what is assigned, it must increase the size. This will reduce its performance.
  • StringBuilder is better when you have to format and concatenate a string more than five times.
  • The most common operation with a string is concatenation. This activity has to be performed very efficiently. When we use the "String" object to concatenate two strings, the first string is combined to the other string by creating a new copy in the memory as a string object, and then the old string is deleted. This process is a little long. Hence we say "Strings are immutable".
  • When we make use of the "StringBuilder" object, the Append method is used. This means, an insertion is done on the existing string. Operation on StringBuilder object is faster than String operations, as the copy is done to the same location. Usage of StringBuilder is more efficient in case large amounts of string manipulations have to be performed.

No comments :

Post a Comment