Visual Basic.NET 2008 > Handling Strings, Characters and Dates

Handling Strings, Characters and Dates in Visual Basic 2008

Jump to Table of Contents

This chapter is a formal discussion of the .NET Framework’s string and date manipulation capabilities. We have used strings extensively in previous chapters, and you already know many of the properties and methods of the String class. Almost every application manipulates strings, so String and StringBuilder are two classes that you’ll use more than any other.

Previous versions of Visual Basic provided numerous functions formanipulating strings. These functions are supported by VB 2008, and not just for compatibility reasons; they’re part of the core of Visual Basic. The string-manipulation functions are still a major part of Visual Basic. Another group of functions deals with dates. The date-manipulation functions are also part of the core of the language and were not moved to a special class. Many of these functions are duplicated in the DateTime class, in the form of properties and methods.

In this chapter, you’ll learn how to do the following:

  • Use the Char data type to handle characters - The Char data type, which is implemented with the Char class, exposes methods for handling individual characters (IsLetter, IsDigit, IsSymbol, and so on). We use the methods of the Char class to manipulate users’ keystrokes as they happen in certain controls (mostly the TextBox control) and to provide immediate feedback.

  • Use the String data type to handle strings - The String data type represents strings and exposes members for manipulating them. Most of the String class’s methods are equivalent to the string-manipulation methods of Visual Basic. The members of the String class are shared: they do not modify the string to which they’re applied. Instead, they return a new string.

  • Use the StringBuilder class to manipulate large or dynamic strings - The StringBuilder class is very efficient at manipulating long strings, but it doesn’t provide as many methods for handling strings. The StringBuilder class provides a few methods to insert, delete, and replace characters within a string. Unlike the equivalent methods of the String class, these methods act directly on the string stored in the current instance of the String-Builder class.

  • Use the DateTime and TimeSpan classes to handle dates and times - The Date class represents dates and time, and it exposes many useful shared methods (such as the IsLeap method, which returns True if the year passed to the method as an argument is leap; the DaysInMonth method; and so on). It also exposesmany instance methods (such as AddYears, AddDays, AddHours, and so on) for adding time intervals to the current instance of the Date class, as well as many options for formatting date and time values.

    The TimeSpan class represents time intervals — from milliseconds to days — with the FromDays, FromHours, and even FromMilliseconds methods. The difference between two date variables is a TimeSpan value, and you can convert this value to various time units by using methods such as TotalDays, TotalHours, TotalMilliseconds, and so on. You can also add a TimeSpan object to a date variable to obtain another date variable.

Get Started >>


Table of Contents

  1. Handling Strings, Characters and Dates
  2. Handling Strings and Characters
  3. The Char Class
  4. The String Class
  5. The StringBuilder Class
  6. The StringReversal Project (Example)
  7. The CountWords Project (Example)
  8. The DateTime Class
  9. The TimeSpan Class Copyright 2011 All Rights Reserved
  Home | Useful links | Contact us | Privacy Policy