Home > .NET > String Optimization in VB.NET

String Optimization in VB.NET

This article describes a set of tips and tricks for using strings in vb.net that will boost the performance of your application especially
when the code contains lots of string related operations such as string concatenation.

String Declaration

The best way to declare a string variable is to set it to an empty string directly after declaration. It is a common mistake for most .net
developers to set the string to “” or Nothing. This is wrong because the “” is not really an empty string for the .net CLR and
the Nothing could throw a NullReferenceException if you reference the string later in the code. Below is the correct code for string declaration :

Dim str As String = “” ‘ wrong

Dim str2 As String = Nothing ‘ wrong

Dim str3 As String = vbNullString ‘ wrong

Dim str4 As String = String.Empty ‘ correct

String Concatenation

The usual way to concatenate strings is to use the + or the & operators. However the & is faster than the +
because it is specially designed for strings while the + also works for numeric addition.

Dim str As String = String.Empty

str = “Hello ” & ” world!” & vbCrLfFor i As Integer = 0 To 4

str &= str & vbCrLf

Next

Optimizing String Concatenation

A similar class to String is called StringBuilder located in System.Text.StringBuilder. The StringBuilder is specially
designed for extreme boosting of string concatenation. It is handled in a special way by the .net CLR. It’s easy to use and manipulate just like
the string class. If you know how much the string length will approximately be at the end of concatenation, you can set this capacity in the constructor
of the StringBuilder which gives additional performance boost:

Dim str As String = “Hello World” & vbCrLf

Dim sb As New System.Text.StringBuilder(str.Length * 4)

For i As Integer = 0 To 4

sb.Append(str)

Next

Dim final As String = sb.ToString ‘get the result string

StringBuilder also supports formatting strings using the AppendFormat method which is also highly optimized and easy to use:

Dim names As New StringCollection

names.Add(“Mike”)

names.Add(“Stacy”)

names.Add(“Bill”)

names.Add(“Krystelle”)Dim sb As New System.Text.StringBuilder

For Each name As String In names

sb.AppendFormat(“My name is {0}”, name)

Next

Dim final As String = sb.ToString

There is no need to instantiate multiple StringBuilder within the same procedure. You can reuse the same
StringBuilder object by setting the capacity property to 0 which deletes the current string:

Dim sb As New System.Text.StringBuilder

‘use sb…

sb.Capacity = 0

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: