Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the length of the StringBuilder in C#?
The StringBuilder class in C# provides the Length property to determine the number of characters currently stored in the StringBuilder object. This property is essential for string manipulation operations and memory management.
Syntax
Following is the syntax to get the length of a StringBuilder −
int length = stringBuilder.Length;
Understanding Length vs Capacity
It's important to distinguish between Length and Capacity of a StringBuilder −
| Property | Description |
|---|---|
| Length | Number of characters currently stored in the StringBuilder |
| Capacity | Maximum number of characters the StringBuilder can hold without reallocating memory |
Example - Basic Length Operations
using System;
using System.Text;
public class Demo {
public static void Main(string[] args) {
StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder(50);
Console.WriteLine("StringBuilder with 'Hello':");
Console.WriteLine("Length = " + sb1.Length);
Console.WriteLine("Capacity = " + sb1.Capacity);
Console.WriteLine("\nEmpty StringBuilder:");
Console.WriteLine("Length = " + sb2.Length);
Console.WriteLine("Capacity = " + sb2.Capacity);
Console.WriteLine("\nStringBuilder with initial capacity 50:");
Console.WriteLine("Length = " + sb3.Length);
Console.WriteLine("Capacity = " + sb3.Capacity);
}
}
The output of the above code is −
StringBuilder with 'Hello': Length = 5 Capacity = 16 Empty StringBuilder: Length = 0 Capacity = 16 StringBuilder with initial capacity 50: Length = 0 Capacity = 50
Example - Length Changes with String Operations
using System;
using System.Text;
public class Demo {
public static void Main(string[] args) {
StringBuilder sb = new StringBuilder("C#");
Console.WriteLine("Initial: '" + sb.ToString() + "' - Length = " + sb.Length);
sb.Append(" Programming");
Console.WriteLine("After Append: '" + sb.ToString() + "' - Length = " + sb.Length);
sb.Insert(0, "Learning ");
Console.WriteLine("After Insert: '" + sb.ToString() + "' - Length = " + sb.Length);
sb.Remove(0, 9);
Console.WriteLine("After Remove: '" + sb.ToString() + "' - Length = " + sb.Length);
sb.Clear();
Console.WriteLine("After Clear: '" + sb.ToString() + "' - Length = " + sb.Length);
}
}
The output of the above code is −
Initial: 'C#' - Length = 2 After Append: 'C# Programming' - Length = 14 After Insert: 'Learning C# Programming' - Length = 23 After Remove: 'C# Programming' - Length = 14 After Clear: '' - Length = 0
Conclusion
The Length property of StringBuilder returns the current number of characters stored in the object. This property is read-only and automatically updates as you perform string operations like Append, Insert, or Remove, making it essential for dynamic string manipulation in C#.
