String and StringBuilder are used to store and manipulate text. The main difference is that String is immutable, while StringBuilder is mutable, making them suitable for different text-processing operations.
- String is suitable for text that does not require frequent modifications.
- StringBuilder is useful when text needs to be modified repeatedly, such as inside loops.
String
A String in is an immutable sequence of characters represented by the System.String class. Once a string object is created, its contents cannot be changed, and operations that modify the text produce a new string.
- Immutable: String operations such as concatenation and replacement do not modify the original string object.
- Easy to Use: Supports built-in methods such as Substring(), Replace(), ToUpper(), and Trim().
Example: The following program demonstrates string concatenation in C#.
using System;
class Geeks{
static void Main()
{
string str = "Hello";
// Concatenate a new value
str = str + " World";
Console.WriteLine(str);
}
}
Output
Hello World
Explanation:
- Initially, str references the string "Hello".
- The concatenation operation produces a new string "Hello World" because strings are immutable.
- The variable str is updated to reference the new string.
StringBuilder
StringBuilder is a mutable sequence of characters provided by the System.Text.StringBuilder class. It allows text to be modified without creating a new string object for every operation.
- Mutable: Supports operations such as Append(), Insert(), Remove(), and Replace() on the same StringBuilder object.
- Efficient for Repeated Changes: Reduces intermediate string allocations when performing multiple text modifications.
Example: The following program demonstrates text modification using StringBuilder.
using System;
using System.Text;
class Geeks{
static void Main(){
StringBuilder sb = new StringBuilder("Hello");
// Append text
sb.Append(" World");
Console.WriteLine(sb);
}
}
Output
Hello World
Explanation:
- new StringBuilder("Hello") creates a mutable StringBuilder object.
- The Append() method adds " World" to the existing StringBuilder object.
- Unlike String concatenation, this operation does not create a new StringBuilder object, although its internal buffer may be resized.
String and StringBuilder
The following table highlights the key differences between String and StringBuilder in C#.
Feature | String | StringBuilder |
|---|---|---|
Namespace | Belongs to System. | Belongs to System.Text. |
Mutability | Immutable; its contents cannot be changed. | Mutable; its contents can be changed. |
Modification | Creates a new string when text changes. | Modifies the existing object. |
Performance | Suitable for occasional text modifications. | Efficient for repeated text modifications. |
Memory Usage | Repeated modifications may allocate new strings. | Uses a reusable buffer to reduce allocations. |
Thread Safety | Safe to share because it is immutable. | Not inherently thread-safe. |
Concatenation | Uses the + operator or Concat(). | Uses Append() or AppendLine(). |
Common Methods | Provides Substring(), Trim(), and Replace(). | Provides Append(), Insert(), and Remove(). |
Best Use Case | Best for fixed text or simple operations. | Best for frequently changing or dynamically built text. |