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:
using System;
using System.Text;
class GFG {
public static void Main(String[] args)
{
StringBuilder str = new StringBuilder( "WelcomeGeeks" );
Console.WriteLine( "String = "
+ str.ToString());
int length = str.Length;
Console.WriteLine( "length of String = "
+ length);
}
}
|
Output:
String = WelcomeGeeks
length of String = 12
Example 2:
using System;
using System.Text;
class GFG {
public static void Main(String[] args)
{
StringBuilder str = new StringBuilder( "India is Great" );
Console.WriteLine( "String = "
+ str.ToString());
int length = str.Length;
Console.WriteLine( "length of String = "
+ length);
}
}
|
Output:
String = India is Great
length of String = 14
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Jan, 2019
Like Article
Save Article