find and solve || findandsolve.com
Please wait.....

String vs Stringbuilder

Difference Between String and StringBuilder


String in C#

The String is immutable in C#,couldn’t modify it after it is created.It always create new object of string type in memory.

For Example
String str = "String Example";
// creates a new string instance
str += "Hello";
str += "World";


String is in c#

String class is immutable
Consumes more memory during concatenation.
Slow performance compare to StringBuilder.
Overrides equals() and hashcode() methods of the object class.
Under System.Text namespace
Thread-safe
Read only instance

Once created, it cannot be modified
String str = “Hello String” 
str = “Hello”


StringBuilder in C#

StringBuilder is mutable string of characters in C#.That means the string which can be changed.It will not create a new modified instance of the current string object.
StringBuilder is provided by StringBuilder class which is present in "System.Text" namespace.Without creating new instance for every time for insert,replace or append so it doesn't create new space in memory.

StringBuilder is in c#

⦁ StringBuffer class is mutable (modifiable).
Consumes less memory during concatenation.
Fast performance compare to String.
Doesn't override the equals() and hashcode() methods of the object class.
Under System namespace
⦁ Shows better performance since new changes are made to an existing instance

For Example

     StringBuilder obj = new StringBuilder("");
    obj.Append("This is first ");
    obj.Append("This is second ");
    obj.Append("This is thrird");
    String s = obj.ToString();
String and StringBuilder Full Example like that:

using System; 
using System.Text; 
using System.Collections;   
class FullExample
{
    // Concatenates to String 
    public static void Address(String f1)
    {
        // taking a string which 
        // is to be Concatenate 
        String str = "forfindandsolve";
        // using String.Concat method 
        // you can also replace it with 
        // s1 = f1 + "forfindandsolve"; 
        f1 = String.Concat(f1, str);
    }

    // Concatenates to StringBuilder 
    public static void Address1(StringBuilder f2)
    {
        // using Append method 
        // of StringBuilder class 
        f2.Append("forfindandsolve");
    }

    // Main Method 
    public static void Main(String[] args)
    {
        String f1 = "findandsolve";
        Address(f1); // f1 is not changed 
        Console.WriteLine("Using String Class: " + f1);
        StringBuilder f2 = new StringBuilder("Geeks");
        Address1(f2); // f2 is changed 
        Console.WriteLine("Using StringBuilder Class: " + f2);
    }
}

Output:
Using String Class: findandsolve
Using StringBuilder Class: forfindandsolve


Converting String to StringBuilder:


using System; 
using System.Text;  
class StringExample
{
    // Main Method 
    public static void Main(String[] args)
    {
        String str = "Findandsolve";
        // conversion from String object 
        // to StringBuilder 
        StringBuilder strBuiler = new StringBuilder(str);
        strBuiler.Append("Findandsolve");
        Console.WriteLine(strBuiler);
    }
}

Output:
Findandsolve

Converting StringBuilder to String:


using System; 
using System.Text;   
class ConvertStringBuilerToString
{

    // Main Method 
    public static void Main(String[] args)
    {

        StringBuilder stringBuilder = new StringBuilder("FindAndSolve");
        // conversion from StringBuilder 
        // object to String using ToString method 
        String str = stringBuilder.ToString();
        Console.Write("StringBuilder object to String: ");
        Console.WriteLine(str);
    }
}

Output:
StringBuilder object to String: FindAndSolve

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response