String Class vs. Microsoft.VisualBasic Namespace - Part II
August 13th, 2007
In this continuation of my series comparing the .NET String class to the string functions Microsoft.VisualBasic we will take a look at some additional functions and correct an oversight from the first article in this series.
String.TrimEnd and String.TrimStart
In my previous discussion, I had said that the String Class didn’t have an RTrim and LTrim type function. I was wrong. I had overlooked these two functions. These two functions provide the same functionality plus having the overload for a character array of values to trim. Given my other observations from my previous article, I’ll now have to say there is little reason to use LTrim or RTrim except backward compatibility.
Asc and Chr
These two functions have been a mainstay of BASIC development for several decades. Asc returns the character code for a string value while Chr does the opposite, returning a string for the specified character code. There are also the ‘W’ versions of these functions that do the same for Unicode characters sets. But, what does the String class offer in this area? As far as I can tell, it doesn’t offer a direct replacement, at least not an obvious one (if you know of one, please leave me a comment and let me know too).
Instr and InStrRev
Instr is another longstanding BASIC function while InStrRev is new, having been introduced in VB6. They both return the position of the first occurrence of one string within another string, although going from different directions. What is the System.String equivalent to these functions and how does it compare?
System.String uses the IndexOf and LastIndexOf functions to do the same thing. In addition to these functions, you also have the IndexOfAny function that gives you even more options. The primary difference between the two functions is that, as expected, the System.String function return value is zero based while the Microsoft.VisualBasic functions are one based.
What does the performance look like? In my testing I found the performance of these functions to be almost identical. In long running test loops one version of the function would be better while in another run the other would do better.
So, which should you use? I think that in the long run a VB.NET programmer will be better off to use the System.String functions since using a zero base consistently will prevent nasty one-off bugs from entering code. However, it is OK to keep old reliable Instr around for legacy code since it doesn’t seem to incur any performance penalty.
Format Functions
Format is another function that has been around for ages in BASIC. In VB6 Microsoft added some new formatting functions for currency, dates, and numbers. System.String has its own Format function. So, which is better?
In this case, System.String has the Microsoft.VisualBasic namespace soundly beaten. Its function routines are more powerful, thanks to overloads that allow you more formatting flexibility. Plus, it is about 20-30% faster since it avoids costly type conversions. Thus, my recommendation would be to always use the System.String Format function. Given the conversion performance hit, you may even want to change your legacy code if you can.
That’s all for this installment. There are some other functions I’ll be looking at in a third and final part coming up later this week. As always, if you spot anything inaccurate or if you have questions, please feel free to leave me a comment. Your participation is appreciated.
Entry Filed under: VB.NET Tutorials
Rate This Article:











Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed