.NET 4.0- A new method StringBuilder.Clear() to clear stringbuilder

Posted By  jalpesh On 10 May 2010 08:05:10
emailbookmarkadd commentsprint
No of Views:722
Bookmarked:0 times
Votes:0 times

Introduction

With Microsoft.NET 4.0 we have a convenient method called clear method which will clear string builder. This method is very use full where we need to use string builder for multiple string manipulation. So we don’t need to create a separate string builder for it. Let’s take a simple example which will print string builder string before,after clear so we can see how it works. Following is simple console application for this.

namespace ConsoleApplication1
{
  class Program
  {
      static void Main(string[] args)
      {
          System.Text.StringBuilder myStringBuilder =
     new System.Text.StringBuilder(string.Empty);
          myStringBuilder.Append("This is my string");
          Console.WriteLine("String Builder Output Before Clear:{0}",
     myStringBuilder.ToString());
          myStringBuilder.Clear();
          Console.WriteLine("String Builder Output After Clear:{0}",
     myStringBuilder.ToString());
          myStringBuilder.Append("This is my another string");
          Console.WriteLine("String Builder Output After new string:{0}",
     myStringBuilder.ToString());
      }
  }
}

 

Snapshot

Image Loading

Happy coding and enjoy with new features.

Sign Up to vote for this article
Other popular Tips/Tricks
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top