Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to find the length of the StringBuilder in C#

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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);
    }
}


Output:

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);
    }
}


Output:

String = India is Great
length of String = 14

Reference:


My Personal Notes arrow_drop_up
Last Updated : 28 Jan, 2019
Like Article
Save Article
Similar Reads