Introduction to System.EnumWelcome to Math Class

3 Quick String Tips

September 18th, 2007

If Me.LastIndexOf("Cherry Limeade") = 0 ThenMy articles on using Strings in VB.NET have been rather popular and appreciated so here’s another one. This time we’ll look at 3 quick and useful tips for using Strings. These are simple but they’re often overlooked, particularly by those new to VB.NET. Hopefully these tips will help you prevent errors in your code and improve performance.

1. Watch Your String Length and Indexes

Previous versions of VB and BASIC in general used one based strings for functions. Some of the Microsoft.VisualBasic Namespace String functions, such as Mid, still are this way. However, if you’re working with the .NET string functions you must always keep in mind that you’re really dealing with a zero based character array behind the scenes. This means that any index value you use must be zero or greater and can’t be greater than or equal to the length of the string. Always check your length before performing string functions like SubString to prevent costly exceptions.

2. Nothing vs. String.Empty

Do you know the difference between a string variable equal to Nothing and one equal to an empty string? Nothing means that the variable does not have an object associated with it while String.Empty means that there is an object that is set to an empty string. Remember that String variables are always Nothing when you declare them unless you initialized them at that point. If you try to use a string variable that has a value of Nothing you will get a Null Reference Exception. I’ve seen this little bug pop up often in property routines and their associated variables.

3. LastIndexOf vs. IndexOf

A common point of confusing with these functions is with the extra parameter overloads where only part of a string is being considered. First of all, go back to tip #1, know the length of the string so that you won’t get a ArgumentOutOfRange exception. Remember that the IndexOf search moves from the start of the string to the end of the string while LastIndexOf searches from the end to the start. This would mean that the first call below would cause an exception while the second one would be OK.

Dim Test As String = "abcdefghijklmnopabcdefghijklmnop"
Dim Position As Integer
Position = Test.IndexOf("abc", 24, 10) 'throws exception
Position = Test.LastIndexOf("abc", 24, 10) 'OK 

I hope you found these ideas helpful. If you have some of your own you would like to add or if you have questions or observations about these tips, please leave 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: Tip Sheets

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