Saturday, November 12, 2011

How to clear a String Builder?

How to clear a StringBuilder?
By now you probably realized that rhere is no Clear method.

You can use the two lines below, the assumption is that you are going to reuse the StringBuilder several times in your code and you want to temporarily clear it to ensure is empty and ready for a new assignment.

sbSQL.Length = 0;
sbSQL.Capacity = 0;

This trims the contents but also shortens the capacity to zero. I was surprised to noticed that both Length and Capacity properties are writable.

Hope it helps,

No comments:

Post a Comment