X



Home>Tutorial>

Difference Between String and StringBuilder in ASP.Net

Nuelson Penuel Saturday, August 29, 2020 ASP.Net

Summary

String is slower as compare to string builder object when we deals with large size of strings. StringBuilder is faster as compare to string object when we deals with large size of strings. String is efficient when we deals with static or smaller size of string meaning we don’t need to modify it later. StringBuilder is efficient when we deals with dynamic or larger size of string meaning we want to modify that string later on.

What is String?

String is immutable. The immutable in this context means you can't modify it after it is created. It creates a new object of string type in memory if you will perform any operation. It belongs to the System.String Namespace. It is also known as sequential collection of Unicode characters that represents text.

String VB.Net Example


Dim strEx As String = "Welcome" 'creates a new string instance
strEx += "To"
strEx += "Emmason Community”
Dim res As String = strEx

String C#.Net Example

C#.Net

string strEx = "Welcome"; //creates a new string instance
strEx += "To";
strEx += "Emmason Community";
string res = strEx;

What is StringBuilder?

StringBuilder is mutable. This means that if an operation is performed on the string, it will not create a new instance every time. With that, it will not create new space in memory, unlike Strings. A StringBuilder belongs to the System.Text.StringBuilder Namespace/class. It represents a mutable string of characters. This class cannot be inherited.

StringBuilder VB.Net Example


Dim strEx  As StringBuilder = new StringBuilder("")
strEx.Append("Welcome")
strEx.Append("To")
strEx.Append("Emmason Community")
Dim res  As String = strEx.ToString();

StringBuilder C#.Net Example


StringBuilder strEx = new StringBuilder("");
strEx.Append("Welcome");
strEx.Append("To");
strEx.Append("Emmason Community");
string res = strEx.ToString();

| | Views(464)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services