How to find the length of the StringBuilder in C#
StringBuilder.Length Property is used to get or set the length of the current StringBuilder object.
Syntax: public int Length { get; set; }
It returns the length of the current instance.Exception: This property will give ArgumentOutOfRangeException if the value specified for a set operation is less than zero or greater than MaxCapacity.
Below programs illustrate the use of the above-discussed property:
Example 1:
// C# program to demonstrate // the Length() Property using System; using System.Text; class GFG { // Main Method public static void Main(String[] args) { // create a StringBuilder object // with a String passed as parameter StringBuilder str = new StringBuilder("WelcomeGeeks"); // print string Console.WriteLine("String = " + str.ToString()); // get length of StringBuilder object int length = str.Length; // print length Console.WriteLine("length of String = " + length); } } |
String = WelcomeGeeks length of String = 12
Example 2:
// C# program to demonstrate // the Length() Property using System; using System.Text; class GFG { public static void Main(String[] args) { // create a StringBuilder object // with a String passed as parameter StringBuilder str = new StringBuilder("India is Great"); // print string Console.WriteLine("String = " + str.ToString()); // get length of StringBuilder object int length = str.Length; // print length Console.WriteLine("length of String = " + length); } } |
String = India is Great length of String = 14
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder.length?view=netframework-4.7.2
Recommended Posts:
- How to find the MaxCapacity of a StringBuilder in C#
- How to find the Capacity of a StringBuilder in C#
- StringBuilder in C#
- How to find the length of an Array in C#
- How to create the StringBuilder in C#
- C# | String vs StringBuilder
- StringBuilder.EnsureCapacity() Method in C#
- StringBuilder.CopyTo Method in C#
- StringBuilder.ToString Method in C#
- C# | Creating StringBuilder having specified capacity
- How to remove all characters from StringBuilder in C#
- StringBuilder.Chars[] Property in C#
- C# | Check if two StringBuilder objects are Equal
- How to set the Length of the Characters in TextBox in C#?
- How to set the Length of the Characters in the ComboBox in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



