Professional Documents
Culture Documents
12 StringBuilder
12 StringBuilder
StringBuilder
1
Contents
What is StringBuilder?
Creating StringBuilder
String Builder
2
What is StringBuilder?
It is used to create variables to hold any text, a sequential collection of characters based on our requirements.
It is mutable.
After creating, the same instance will be used to perform any operations like inserting, appending, removing, or replacing the
It is a dynamic object which will expand a memory dynamically to accommodate the modifications of string instead of
Heap
Initial Value
Modified Value
4
Difference between String and StringBuilder
Only difference between string and stringbuilder :
Strings are immutable
StringBuilder is mutable
6
Length Property
“Length” is used to get or set the length of the current StringBuilder object.
Console.WriteLine(sb[i]);
7
StringBuilder Methods
Method Description
StringBuilder.Append This method will append the given string value to the end of the current
StringBuilder.
StringBuilder.AppendFormat It will replace a format specifier passed in a string with formatted text.
8
StringBuilder Methods
Append ( )
AppendLine( )
used to add or append a string object at the end of the append a string with the newline character at the end
string represented by the StringBuilder
Signature: Signature:
9
StringBuilder Methods
AppendFormat ( )
used to add or append string objects by formatting them into a specified format at the end of the string represented by
the StringBuilder
Signature:
10
StringBuilder Methods
Insert( )
used to insert a string at the specified index position of the current StringBuilder object
Signature:
11
StringBuilder Methods
Remove ( )
Replace( )
used to remove a specified number of characters from used to replace all occurrences of specified string characters
the current StringBuilder object, starting from the in the current StringBuilder object with a specified
specified index position replacement string character
Signature: Signature:
public StringBuilder Remove (int startindex , int length) public StringBuilder Replace (string oldValue, string
newValue)
12
StringBuilder Methods
ToString()
convert a StringBuilder object to a string
Signature:
It takes no parameter.
It returns String object.
13
Thank You
14