0% found this document useful (0 votes)
27 views14 pages

12 StringBuilder

Uploaded by

lion man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

12 StringBuilder

Uploaded by

lion man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

University of Computer Studies

StringBuilder

1
Contents

 What is StringBuilder?

 Creating StringBuilder

 Difference between String and

String Builder

 Property of StringBuilder Class

 Methods of StringBuilder Class

2
What is StringBuilder?

 It is a class that is useful to represent a mutable string of characters.

 It is used to create variables to hold any text, a sequential collection of characters based on our requirements.

 It is mutable.

 Mutability means once an instance of the class is created.

 After creating, the same instance will be used to perform any operations like inserting, appending, removing, or replacing the

characters instead of creating a new instance for every time.

 It is a dynamic object which will expand a memory dynamically to accommodate the modifications of string instead of

creating a new instance in the memory.

 need to import the System.Text namespace 3


Creating StringBuilder

StringBuilder Declaration and Initialization RAM


StringBuilder sb = new StringBuilder(“Hello”); Stack

sb.Append(“ Welcome To”);

Heap
Initial Value

Hello Welcome To 0x70000

Modified Value

4
Difference between String and StringBuilder
 Only difference between string and stringbuilder :
 Strings are immutable
 StringBuilder is mutable

string st = “Hello”; StringBuilder sb = new StringBuilder(“Hello”);


st = st + “ Welcome To”; sb.Append(“ Welcome To”);
RAM
RAM
Code
Code
Initial string
+
Stack New or
Stack New or
Modified
Initial string Modified
string
Heap
Heap
Hello
Hello +
Welcome To 5
Memory allocation
Hello Welcome forTo
string Memory allocation for stringbuilder
Capacity Property
 can specify an initial capacity of characters
 new space will be allocated automatically
 the capacity of StringBuilder will be doubled
 default capacity of StringBuilder is 16 characters
 its maximum capacity is more than 12 billion characters

StringBuilder sb = new StringBuilder(25);


StringBuilder sb = new StringBuilder("Welcome to Tutlane",25);
sb.Capacity = 25;

6
Length Property

 “Length” is used to get or set the length of the current StringBuilder object.

 It returns the length of the current instance.

StringBuilder sb = new StringBuilder("Hello World!");

for(int i = 0; i < sb.Length; i++)

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.

StringBuilder.Insert It inserts a string at the specified index of the current StringBuilder.

StringBuilder.Remove It removes a specified number of characters from the current


StringBuilder.

StringBuilder.Replace It replaces a specified character at a specified index.

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:

public StringBuilder Append(string st) public StringBuilder Append(string st)

 It takes one parameter.


 It return StringBuilder object.  It takes one parameter.
 It return StringBuilder object.

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:

public StringBuilder AppendFormat (string format, Object arg)

 It takes tow parameters.


 It returns StringBuilder object.

10
StringBuilder Methods

Insert( )
 used to insert a string at the specified index position of the current StringBuilder object

Signature:

public StringBuilder Insert (int index, string value)

 It takes tow parameters.


 It returns StringBuilder object.

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)

 It takes tow parameters.


 It returns StringBuilder object.  It takes tow parameters.
 It returns StringBuilder object.

12
StringBuilder Methods

ToString()
 convert a StringBuilder object to a string
Signature:

public string ToString( )

 It takes no parameter.
 It returns String object.

13
Thank You

14

You might also like