I want to test if the StringBuilder
is empty but there is no IsEmpty
method or property.
How does one determine this?
I want to test if the StringBuilder
is empty but there is no IsEmpty
method or property.
How does one determine this?
If you look at the documentation of StringBuilder it has only 4 properties. One of them is Length
.
The length of a StringBuilder object is defined by its number of Char objects.
You can use the Length property:
Gets or sets the length of the current StringBuilder object.
StringBuilder sb = new StringBuilder();
if (sb.Length != 0)
{
// you have found some difference
}
Another possibility would be to treat it as a string by using the String.IsNullOrEmpty method and condense the builder to a string using the ToString
method. You can even grab the resulting string and assign it to a variable which you would use if you have found some differences:
string difference = "";
if (!String.IsNullOrEmpty(difference = sb.ToString()))
{
Console.WriteLine(difference);
}
use the StringBuilder.Length
Property, here the doc
if (mySB.Length > 0)
{
Console.WriteLine("Bang! is not empty!");
}
Just use Length. Here is how to use it:
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("Hello World");
// print string
System.out.println("String = "
+ str.toString());
// get length of StringBuilder object and initialize if you use it more than ones
int length = str.length();
// you can check like
if(len != 0)
// print length
System.out.println("length of String = "
+ length);
Use this, it will work:
StringBuilder stringbuilder = new StringBuilder();
if(string.isnullorempty(Convert.toString(stringbuilder)))