
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to find the length of the StringBuilder in C#?
To find the length of the StringBuilder in C#, the code is as follows −
Example
using System; using System.Text; public class Demo { public static void Main(String[] args) { StringBuilder strBuilder1 = new StringBuilder("Amy"); StringBuilder strBuilder2 = new StringBuilder("Katie"); StringBuilder strBuilder3 = new StringBuilder(); StringBuilder strBuilder4 = new StringBuilder(5); strBuilder2 = strBuilder3; Console.WriteLine("Is StringBuilder3 equal to StringBuilder2? = "+strBuilder3.Equals(strBuilder2)); Console.WriteLine("StringBuider1 capacity = "+strBuilder1.Capacity); Console.WriteLine("StringBuider1 maximum capacity = "+strBuilder1.MaxCapacity); Console.WriteLine("StringBuider1 length = "+strBuilder1.Length); Console.WriteLine("StringBuider2 capacity = "+strBuilder2.Capacity); Console.WriteLine("StringBuider2 maximum capacity = "+strBuilder2.MaxCapacity); Console.WriteLine("StringBuider2 length = "+strBuilder2.Length); Console.WriteLine("StringBuider3 capacity = "+strBuilder3.Capacity); Console.WriteLine("StringBuider3 maximum capacity = "+strBuilder3.MaxCapacity); Console.WriteLine("StringBuider3 length = "+strBuilder3.Length); Console.WriteLine("StringBuider4 capacity = "+strBuilder4.Capacity); Console.WriteLine("StringBuider4 maximum capacity = "+strBuilder4.MaxCapacity); Console.WriteLine("StringBuider4 length = "+strBuilder4.Length); } }
Output
This will produce the following output −
Is StringBuilder3 equal to StringBuilder2? = True StringBuider1 capacity = 16 StringBuider1 maximum capacity = 2147483647 StringBuider1 length = 3 StringBuider2 capacity = 16 StringBuider2 maximum capacity = 2147483647 StringBuider2 length = 0 StringBuider3 capacity = 16 StringBuider3 maximum capacity = 2147483647 StringBuider3 length = 0 StringBuider4 capacity = 5 StringBuider4 maximum capacity = 2147483647 StringBuider4 length = 0
Example
Let us see another example −
using System; using System.Text; public class Demo { public static void Main(String[] args) { StringBuilder strBuilder1 = new StringBuilder("hbjhbkjhbm"); StringBuilder strBuilder2 = new StringBuilder("TomHanks"); StringBuilder strBuilder3 = new StringBuilder(); Console.WriteLine("Capacity of StringBuilder1 = "+strBuilder1.Capacity); Console.WriteLine("Length of StringBuilder1 = "+strBuilder1.Length); Console.WriteLine("
Capacity of StringBuilder2 = "+strBuilder2.Capacity); Console.WriteLine("Length of StringBuilder2 = "+strBuilder2.Length); Console.WriteLine("
Capacity of StringBuilder3 = "+strBuilder3.Capacity); Console.WriteLine("Length of StringBuilder3 = "+strBuilder3.Length); } }
Output
This will produce the following output −
Capacity of StringBuilder1 = 16 Length of StringBuilder1 = 10 Capacity of StringBuilder2 = 16 Length of StringBuilder2 = 8 Capacity of StringBuilder3 = 16 Length of StringBuilder3 = 0
- Related Articles
- How to find the MaxCapacity of a StringBuilder in C#?
- How to find the Capacity of a StringBuilder in C#
- How to create the StringBuilder in C#?
- How To Find the Length of Hypotenuse in Java?
- How to find the length of an array in JavaScript?
- How to find the length of sequence vector in R?
- How to find the length of a string in TypeScript?
- How to use StringBuilder in C#?
- What is the purpose of the StringBuilder class in C#?
- How to find the length of columns for missing values in R?
- How to find the length of jagged array using a property?
- How to find the length and rank of a jagged array in C#?
- How to find the length of the largest string in an R data frame column?
- Append to StringBuilder in C#
- How do you find the length of an array in C#?

Advertisements