Click here to Skip to main content
12,954,631 members (62,211 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 24 Dec 2009

String Optimization in VB.NET

, 24 Dec 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
String optimization methods and best practices 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 applications, especially when the code contains lots of string related operations such as string concatenation. The original source of this article is taken from The home of all .NET developers - String Optimization in VB.NET. I recommend that you read those useful articles related to the .NET framework, especially the ones that are concerned with optimization, before you go on with this article.

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 a string to "" or Nothing. This is wrong because "" is not really an empty string for .NET CLR, and 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, & is faster than + because it is specially designed for strings while + also works for numeric addition.

Dim str As String = String.Empty
str = "Hello " & " world!" & vbCrLf

For i As Integer = 0 To 4
    str &= str & vbCrLf

Optimizing String Concatenation

A similar class to String is called StringBuilder, located in System.Text.StringBuilder. 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 the concatenation, you can set this capacity in the constructor of the StringBuilder, which gives an 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
Dim final As String = sb.ToString 'get the result string

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

Dim names As New StringCollection

Dim sb As New System.Text.StringBuilder
For Each name As String In names
     sb.AppendFormat("My name is {0}", name)
Dim final As String = sb.ToString

There is no need to instantiate multiple StringBuilders 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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ali Tarhini
Software Developer (Senior) Microgen
Lebanon Lebanon
For more articles and extreme topics please check out my personal website

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Andy Davies30-Dec-09 12:45
memberAndy Davies30-Dec-09 12:45 
GeneralMy vote of 1 [modified] Pin
Ozgur Ozcitak29-Dec-09 9:32
memberOzgur Ozcitak29-Dec-09 9:32 
QuestionAre you the author of another article? Pin
SAKryukov27-Dec-09 15:25
memberSAKryukov27-Dec-09 15:25 
General[My vote of 1] Wrong, questionable statements or just fantasy [modified] Pin
SAKryukov27-Dec-09 15:10
memberSAKryukov27-Dec-09 15:10 
AnswerRe: [My vote of 1] Wrong, questionable statements or just fantasy [modified] Pin
Ali Tarhini27-Dec-09 20:29
memberAli Tarhini27-Dec-09 20:29 
GeneralYour disassemly does not disprove my point Pin
SAKryukov1-Jan-10 14:40
memberSAKryukov1-Jan-10 14:40 
GeneralMy vote of 1 Pin
onclethomas25-Dec-09 20:01
memberonclethomas25-Dec-09 20:01 
GeneralMy vote of 1 Pin
Md. Marufuzzaman25-Dec-09 7:55
groupMd. Marufuzzaman25-Dec-09 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 24 Dec 2009
Article Copyright 2009 by Ali Tarhini
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid