The StringBuilder Class
Solve issues related to immutability using StringBuilder.
We'll cover the following
Introduction
The System.String
namespace offers a plethora of methods for handling text. We could even create our own text editor. Remember, though, that a new string is created any time we make a change to a string object. That’s the nature of strings, they are immutable. This can be a problem if our program handles large texts and makes many changes. Memory allocation is a costly operation in terms of memory and processing power.
There must be something dynamic and mutable, something that can be modified in-place instead of created from scratch. Fortunately, .NET provides the StringBuilder
class, which is like a mutable version of System.String
.
Using StringBuilder
Note: The
StringBuilder
class is located in theSystem.Text
namespace.
Mutability is achieved through the allocation more space than is required:
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy