65

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

How does one determine this?

ΩmegaMan
  • 26,526
  • 10
  • 91
  • 107
Boiethios
  • 31,388
  • 12
  • 113
  • 159
  • 14
    `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
  • 1
    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

3 Answers3

87

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
  • 21,868
  • 8
  • 38
  • 71
  • 3
    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
7

use the StringBuilder.Length Property, here the doc

if (mySB.Length > 0)
{
     Console.WriteLine("Bang! is not empty!"); 
}
ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91
-2

Use this, it will work:

StringBuilder stringbuilder = new StringBuilder();
if(string.isnullorempty(Convert.toString(stringbuilder)))
jps
  • 15,760
  • 14
  • 59
  • 71