Click here to Skip to main content
Click here to Skip to main content

String Optimization in VB.NET

By , 24 Dec 2009
 

Introduction

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
Next

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
     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 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 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

License

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
Member
For more articles and extreme topics please check out my personal website www.alitarhini.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberAndy Davies30 Dec '09 - 12:45 
GeneralMy vote of 1 [modified]memberOzgur Ozcitak29 Dec '09 - 9:32 
QuestionAre you the author of another article?memberSAKryukov27 Dec '09 - 15:25 
General[My vote of 1] Wrong, questionable statements or just fantasy [modified]memberSAKryukov27 Dec '09 - 15:10 
AnswerRe: [My vote of 1] Wrong, questionable statements or just fantasy [modified]memberAli Tarhini27 Dec '09 - 20:29 
GeneralYour disassemly does not disprove my pointmemberSAKryukov1 Jan '10 - 14:40 
GeneralMy vote of 1memberonclethomas25 Dec '09 - 20:01 
GeneralMy vote of 1groupMd. Marufuzzaman25 Dec '09 - 7:55 

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

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