How To Convert a WinForms App To A Windows ServiceLink Round-Up for 8/16/07

String Class vs. Microsoft.VisualBasic Namespace - Part III

August 15th, 2007

scaleIn this third and final article we will finish up our comparision of the System.String Class to the string functions in the Microsoft.VisualBasic Namespace. If you would like to read the previous articles, read Part I here and Part II here.

Split and Join

These handy string functions move a delimited string to an array or vice versa. Both the System.String and Microsoft.VisualBasic Namespace have these functions available. Is there any reason to prefer one method to the other?

In performance testing, the VB functions lag behind. They’re 40-50% slower than the System.String functions. It looks like this is primarily due to them doing costly object conversions rather than using only straight string values. When we look at the available overloads the System.String functions have the VB version beat there as well with the ability to exclude blank entries and to use an array of delimiters.

When it comes to these functions, System.String is clearly a better choice for your development needs outside of importing legacy code. Even then, there is a significant performance improvement to be found by switching to the new .NET method.


Len, another classic BASIC function, returns the length of a string. System.String has the Length property that provides the same information. Is one better than the other?

Performance-wise, these two functions show little difference over 1000’s of iterations. There doesn’t appear to be any reason to prefer one over the other in this case plus there is no functional difference. I’m kind of partial to using the property value rather than the VB function since it encourages thinking of .NET strings as objects. However, at the core, it’s really only a personal preference thing.


Replace does what it says, it replaces one character, or group of characters, in a string with a replacement string. How do our two namespaces match up with this function?

The performance test showed that the System.String function was about 7-12% faster than the VB function. If you’re doing a replace over many iterations, this means that there is some performance value to using the .NET String function.

When we look at overloads, the Microsoft.VisualBasic Replace has optional parameters for the start, count, and compare method that the System.String version doesn’t have. If your replacement logic needs these additional parameters, then it’s probably worthwhile to consider using this version of the function.

All in all, which one to use in this case depends on your needs. If you need speed, System.String is the ticket. If you need flexibility, Microsoft.VisualBasic provides it.

Space and StrDup

Space is another venerable BASIC function that’s used to generate a blank, space-filled, string. You may also remember its cousin, String$, that filled in any character in the same manner. This function isn’t in .NET, of course, since it would conflict with the String class. Instead, it has been replaced with the StrDup function. The equivalent System.String function to these two methods is using the New method to generate the string, like so:

Dim HundredSpaces As String = New String(" "c, 100)

Where the first parameter is the character value to use (note the c suffix on the string to define it as character value, not string) and the second is the length. Note that unlike the Space function, you can specify any single character or an array of character values.

Now, let’s see which one performs the best. The performance of both methods is virtually identical. No performance penalty is incurred by using the Microsoft.VisualBasic functions. In fact, in some testing, StrDup outperformed New String and Space by about 10% while in other tests they were dead even. Perhaps the StrDup function is implemented differently.

When we look at the overall functionality there are some factors that lean toward using StrDup. It accepts both Char and String values, thus reducing the need for conversion and/or clumsy array syntax. It will do object conversion internally, if you’re running without Option Strict on (not recommended). Plus, its performance is good. Of course, it is a rather obscure replacement function so you may find that sticking with .NET String functions is better than having to explain what it does each time.

That’s all for this series of articles comparing System.String to Microsoft.VisualBasic string functions. Please let me know what you think by leaving me a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials

Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (No Ratings Yet)
Loading ... Loading ...

Leave a Comment


Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles


Most Recent Articles


 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities