81

I want to test if the StringBuilder is empty but there is no IsEmpty method or property.

How does one determine this?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Boiethios
  • 38,438
  • 19
  • 134
  • 183
  • 15
    `sb.Length == 0` ? – Lasse V. Karlsen Aug 01 '17 at 08:53
  • @LasseV.Karlsen Ow, so easy :/ Sorry, I am beginner in C#. – Boiethios Aug 01 '17 at 08:54
  • 2
    The fact that intellisense says "Gets or sets the length of the stringbuilder object', combined with the fact that the constructor takes a capacity as an argument and the fact that one can't SET the length of an existing string all combine to lead one to believe that Length probably reflects capacity. The fact that it's the answer is highly unintuitive. – bielawski Mar 18 '20 at 09:48

4 Answers4

109

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);      
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 4
    It would have been easy to put `public bool Empty { get; } = this.Length == 0;`. Strange they did not have this idea. – Boiethios Aug 01 '17 at 09:07
  • @Boiethios that is true, I always wondered why they did not implement something like this. – Mong Zhu Aug 01 '17 at 09:09
  • And this is the same for the enumerables: https://stackoverflow.com/questions/18867180/check-if-list-is-empty-in-c-sharp – Boiethios Aug 01 '17 at 09:11
  • I think it is because length property is writable. So you can limit the amount of characters the stringbuilder will parse to a string when calling toString method. So I don't even know when to truste kength property. The code posted here is quite good since it realys on the actualoutput of the stringbuilder – Ricker Silva Jul 25 '18 at 19:06
  • 1
    @RickerSilva that is a good point. Actually it´s even worse. If you set the `Length` property then `StringBuilder` will fill it up until that point with `0x00` characters. If you add then adterwards strings they will be appended to the zero hex string. If the `Length` property is set then unfortunately the `string.IsNullOrWhiteSpace(sb.ToString())` or the `string.IsNullOrEmpty(sb.ToString())` approach also stops working – Mong Zhu Jul 26 '18 at 08:40
8

use the StringBuilder.Length Property, here the doc

if (mySB.Length > 0)
{
     Console.WriteLine("Bang! is not empty!"); 
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
-1

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

Use this, it will work:

StringBuilder stringbuilder = new StringBuilder();
if(string.isnullorempty(Convert.toString(stringbuilder)))
jps
  • 20,041
  • 15
  • 75
  • 79