Friday, March 4, 2011

stringbuilder in .net


String concatenation is one of the commonly used operations among programmers. If you don't handle the string concatenation in .NET properly, it may decrease the performance of an application.
You can concatenate strings in two ways -
  • First, traditional way of using string and adding the new string to an existing string. In the .NET Framework, this operation is costly. When you add a string to an existing string, the Framework copies both the existing and new  data to the memory, deletes the existing string, and reads data in a new string. This operation may be very resource consuming in lengthy string concatenation operations.
  • The second and better way to concatenate strings in .NET is using the StringBuilder class. The StringBuidler class provides the Append method, that inserts a new string to an existing string. 
To prove this theory, we will write two routines and both will repeat the same operations. One is using the string class and second is using the StringBuilder class.


Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)



Constructors



NameDescription
Public method Supported by the XNA FrameworkStringBuilder()Initializes a new instance of the StringBuilder class.
Public method Supported by the XNA FrameworkStringBuilder(Int32)Initializes a new instance of the StringBuilder class using the specified capacity.
Public method Supported by the XNA FrameworkStringBuilder(String)Initializes a new instance of the StringBuilder class using the specified string.
Public method Supported by the XNA FrameworkStringBuilder(Int32, Int32)Initializes a new instance of the StringBuilder class that starts with a specified capacity and can grow to a specified maximum.
Public method Supported by the XNA FrameworkStringBuilder(String, Int32)Initializes a new instance of the StringBuilder class using the specified string and capacity.
Public method Supported by the XNA FrameworkStringBuilder(String, Int32, Int32, Int32)Initializes a new instance of the StringBuilder class from the specified substring and capacity.

Methods



NameDescription
Public method Supported by the XNA FrameworkAppend(Boolean)Appends the string representation of a specified Boolean value to this instance.
Public method Supported by the XNA FrameworkAppend(Byte)Appends the string representation of a specified 8-bit unsigned integer to this instance.
Public method Supported by the XNA FrameworkAppend(Char)Appends the string representation of a specified Unicode character to this instance.
Public method Supported by the XNA FrameworkAppend(Char[])Appends the string representation of the Unicode characters in a specified array to this instance.
Public methodAppend(Decimal)Appends the string representation of a specified decimal number to this instance.
Public method Supported by the XNA FrameworkAppend(Double)Appends the string representation of a specified double-precision floating-point number to this instance.
Public method Supported by the XNA FrameworkAppend(Int16)Appends the string representation of a specified 16-bit signed integer to this instance.
Public method Supported by the XNA FrameworkAppend(Int32)Appends the string representation of a specified 32-bit signed integer to this instance.
Public method Supported by the XNA FrameworkAppend(Int64)Appends the string representation of a specified 64-bit signed integer to this instance.
Public method Supported by the XNA FrameworkAppend(Object)Appends the string representation of a specified object to this instance.
Public method Supported by the XNA FrameworkAppend(SByte)Appends the string representation of a specified 8-bit signed integer to this instance.
Public method Supported by the XNA FrameworkAppend(Single)Appends the string representation of a specified single-precision floating-point number to this instance.
Public method Supported by the XNA FrameworkAppend(String)Appends a copy of the specified string to this instance.
Public method Supported by the XNA FrameworkAppend(UInt16)Appends the string representation of a specified 16-bit unsigned integer to this instance.
Public method Supported by the XNA FrameworkAppend(UInt32)Appends the string representation of a specified 32-bit unsigned integer to this instance.
Public method Supported by the XNA FrameworkAppend(UInt64)Appends the string representation of a specified 64-bit unsigned integer to this instance.
Public method Supported by the XNA FrameworkAppend(Char, Int32)Appends a specified number of copies of the string representation of a Unicode character to this instance.
Public method Supported by the XNA FrameworkAppend(Char[], Int32, Int32)Appends the string representation of a specified subarray of Unicode characters to this instance.
Public method Supported by the XNA FrameworkAppend(String, Int32, Int32)Appends a copy of a specified substring to this instance.
Public methodAppendFormat(String, Object)Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a single argument.
Public method Supported by the XNA FrameworkAppendFormat(String,Object[])Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter array.
Public method Supported by the XNA FrameworkAppendFormat(IFormatProvider, String, Object[])Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter array using a specified format provider.
Public methodAppendFormat(String, Object, Object)Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of two arguments.
Public methodAppendFormat(String, Object, Object, Object)Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of three arguments.
Public method Supported by the XNA FrameworkAppendLine()Appends the default line terminator to the end of the current StringBuilder object.
Public method Supported by the XNA FrameworkAppendLine(String)Appends a copy of the specified string followed by the default line terminator to the end of the currentStringBuilder object.
Public methodClearRemoves all characters from the current StringBuilderinstance.
Public methodCopyToCopies the characters from a specified segment of this instance to a specified segment of a destination Chararray.
Public method Supported by the XNA FrameworkEnsureCapacityEnsures that the capacity of this instance ofStringBuilder is at least the specified value.
Public method Supported by the XNA FrameworkEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public method Supported by the XNA FrameworkEquals(StringBuilder)Returns a value indicating whether this instance is equal to a specified object.
Protected method Supported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA FrameworkGetHashCodeServes as a hash function for a particular type.(Inherited from Object.)
Public method Supported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited fromObject.)
Public methodInsert(Int32, Boolean)Inserts the string representation of a Boolean value into this instance at the specified character position.
Public methodInsert(Int32, Byte)Inserts the string representation of a specified 8-bit unsigned integer into this instance at the specified character position.
Public methodInsert(Int32, Char)Inserts the string representation of a specified Unicode character into this instance at the specified character position.
Public method Supported by the XNA FrameworkInsert(Int32, Char[])Inserts the string representation of a specified array of Unicode characters into this instance at the specified character position.
Public methodInsert(Int32, Decimal)Inserts the string representation of a decimal number into this instance at the specified character position.
Public methodInsert(Int32, Double)Inserts the string representation of a double-precision floating-point number into this instance at the specified character position.
Public methodInsert(Int32, Int16)Inserts the string representation of a specified 16-bit signed integer into this instance at the specified character position.
Public methodInsert(Int32, Int32)Inserts the string representation of a specified 32-bit signed integer into this instance at the specified character position.
Public methodInsert(Int32, Int64)Inserts the string representation of a 64-bit signed integer into this instance at the specified character position.
Public methodInsert(Int32, Object)Inserts the string representation of an object into this instance at the specified character position.
Public methodInsert(Int32, SByte)Inserts the string representation of a specified 8-bit signed integer into this instance at the specified character position.
Public methodInsert(Int32, Single)Inserts the string representation of a single-precision floating point number into this instance at the specified character position.
Public method Supported by the XNA FrameworkInsert(Int32, String)Inserts a string into this instance at the specified character position.
Public methodInsert(Int32, UInt16)Inserts the string representation of a 16-bit unsigned integer into this instance at the specified character position.
Public methodInsert(Int32, UInt32)Inserts the string representation of a 32-bit unsigned integer into this instance at the specified character position.
Public methodInsert(Int32, UInt64)Inserts the string representation of a 64-bit unsigned integer into this instance at the specified character position.
Public method Supported by the XNA FrameworkInsert(Int32, String, Int32)Inserts one or more copies of a specified string into this instance at the specified character position.
Public method Supported by the XNA FrameworkInsert(Int32, Char[], Int32, Int32)Inserts the string representation of a specified subarray of Unicode characters into this instance at the specified character position.
Protected method Supported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object.(Inherited from Object.)
Public method Supported by the XNA FrameworkRemoveRemoves the specified range of characters from this instance.
Public method Supported by the XNA FrameworkReplace(Char, Char)Replaces all occurrences of a specified character in this instance with another specified character.
Public method Supported by the XNA FrameworkReplace(String, String)Replaces all occurrences of a specified string in this instance with another specified string.
Public method Supported by the XNA FrameworkReplace(Char, Char, Int32, Int32)Replaces, within a substring of this instance, all occurrences of a specified character with another specified character.
Public method Supported by the XNA FrameworkReplace(String, String, Int32, Int32)Replaces, within a substring of this instance, all occurrences of a specified string with another specified string.
Public method Supported by the XNA FrameworkToString()Converts the value of this instance to a String.(Overrides Object.ToString().)
Public method Supported by the XNA FrameworkToString(Int32, Int32)Converts the value of a substring of this instance to aString.


using System;
using System.Text;

public sealed class App 
{
    static void Main() 
    {
        // Create a StringBuilder that expects to hold 50 characters.
        // Initialize the StringBuilder with "ABC".
        StringBuilder sb = new StringBuilder("ABC", 50);

        // Append three characters (D, E, and F) to the end of the StringBuilder.
        sb.Append(new char[] { 'D', 'E', 'F' });

        // Append a format string to the end of the StringBuilder.
        sb.AppendFormat("GHI{0}{1}", 'J', 'k');

        // Display the number of characters in the StringBuilder and its string.
        Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());

        // Insert a string at the beginning of the StringBuilder.
        sb.Insert(0, "Alphabet: ");

        // Replace all lowercase k's with uppercase K's.
        sb.Replace('k', 'K');

        // Display the number of characters in the StringBuilder and its string.
        Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());
    }
}

// This code produces the following output.
//
// 11 chars: ABCDEFGHIJk
// 21 chars: Alphabet: ABCDEFGHIJK


string sample1 = "go4expert.com";
string sample2 = "Rules";
string sample3;
sample3 = sample1 + " " + sample2;



StringBuilder class


Using system.text;
StringBuilder sb = new StringBuilder("go4expert.com");
Console.WriteLine(sb.ToString());



StringBuilder sb = new StringBuilder("go4expert.com");
sb.Append(" Rules!");



StringBuilder sb = new StringBuilder();
sb.Capacity = 18;
sb.Append("go4expert.com");
sb.Append(" Rules!");



StringBuilder sb = new StringBuilder("go4expert.com Rules!");
sb.length = 11;
Console.WriteLine(sb.ToString());








No comments :

Post a Comment