Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Process string faster with the StringBuilder class

Total Hit ( 2927)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


You can think of a StringBuilder object as a buffer that can contain a string with the ability to grow from zero characters to the buffer's current capacity. Until you exceed that capacity, the string is assembled in the buffer and no object is allocated or released. If the string becomes longer than the current capacity, the StringBuilder object transparently creates a larger buffer. The default buffer initially contains 16 characters, but you can assign a different capacity in the StringBuilder's constructor or by assigning a value to the Capacity property:

Click here to copy the following block
' Create a StringBuilder object with capacity of 1000 characters.
Dim sb As New System.Text.StringBuilder(1000)

You can process the string held in the StringBuilder object with several methods, most of which have same name as and work similarly to methods exposed by the String class-for example, the Insert, Remove, and Replace methods. The most common way to build a string inside a StringBuilder object is by means of its Append method, which takes an argument of any type and appends it to the current internal string:

Click here to copy the following block
' Create a comma-delimited list of the first 100 integers.
Dim n As Integer
For n = 1 To 100
  ' Note that two Append methods are faster than a single Append
  ' whose argument is the concatenation of N and ",".
  sb.Append(n)
  sb.Append(",")
Next
' Insert a string at the beginning of the buffer.
sb.Insert(0, "List of numbers: ")
Console.WriteLine(sb)  ' => List of numbers: 1,2,3,4,5,6,...

Because a StringBuilder doesn't allocate memory (or does it only when the internal buffer isn't long enough for the string being assign), it is much faster. For example, the code above is 100-150 faster than a similar loop based on the standard String class.



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.