Select Page
Codoid Blog

Power of StringBuilder Class

Being a leading QA Company, we write blogs on all prominent software testing topics and tools using our real-world experience. So stay sharp by subscribing to our Newsletter.

What is StringBuilder class? We had this question few years back. We googled it for QTP implementation and found the below article from AdvancedQTP (Our Favorite Site).

VB and VBScript are slow with string operations because each string is stored as a block of memory. If a string length is increased, then it requires a new block of memory and the existing and new content will be accommodated into the new block.

We were stumbled when we read it because the value which is held by StringBuilder can be modified without any new memory allocation. For better understanding of this, we should know where VBScript String operation lacks.

VB and VBScript are slow with string operations because each string is stored as a block of memory. If a string length is increased, then it requires a new block of memory and the existing and new content will be accommodated into the new block.

The problem is: If we increase the string size often or use looping string concatenation,then we will see the performance decrease here. The reason is it does reallocation whenever the string size increases. In QTP script development, most of the time we work with strings. For example: Populating result into HTML file, Reading strings from file and handling the test data, etc.

Whenever we perform a loop string concatenation or strings are concatenated more number of times arbitrarily, we can use StringBuilder class.

Code

'This snippet creates a string containing HTML document using StringBuilder class
Dim oSB

'Creating object for StringBuilder class
Set oSB = DotNetFactory.CreateInstance("System.Text.StringBuilder")

oSB.AppendLine ""
oSB.AppendLine ""
oSB.AppendLine "<table><tbody><tr><td>Col1</td><td>Col2</td></tr></tbody></table>"
oSB.AppendLine ""
oSB.Append ""

'Retrieving the content using ToString method available in StringBuilder class.
Print oSB.ToString

Set oSB=Nothing
  

Synopsis

We can see increase in performance whenever we handle a large number of string operations using StringBuilder class.

References: MSDN

Comments(0)

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility