Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Hour 12. Performing Arithmetic, String M... > Working with Dates and Times - Pg. 283

Working with Dates and Times 283 strFirstName = Trim(Microsoft.VisualBasic.Left(strFullName, intLocation)) Use Trim() in place of RTrim() or LTrim() unless you specifically want to keep spaces at one end of the string. Did you Know? Replacing Text Within a String It's not uncommon to have to replace a piece of text within a string with some other text. Some people still put two spaces between sentences, for example, even though this is no longer necessary because of proportional fonts. You could replace all double spaces in a string with a single space by using a loop and the string manipulation functions discussed so far, but there's an easier way: the Replace() function. A basic Replace() function call has the following syntax: Replace(expression, findtext, replacetext) The expression argument is the text to search, such as a string variable. The findtext argument is used to specify the text to look for within expression . The replacetext argument is used to specify the text used to replace the findtext . Con- sider the following code: Dim strText As String = "Give a man a fish" strText = Replace(strText, "fish", "sandwich") When this code finishes executing, strText contains the string "Give a man a sand- wich". Replace() is a powerful function that can save many lines of code, and you should use it in place of a homegrown replace function whenever possible. Working with Dates and Times Dates are a unique beast. In some ways, they act like strings, where you can concate- nate and parse pieces. In other ways, dates seem more like numbers in that you can add to or subtract from them. You'll often perform math-type functions on dates (such as adding a number of days to a date or determining the number of months between two dates), but you won't use the typical arithmetic operations. Instead, you use functions specifically designed for working with dates. Understanding the Date Data Type Working with dates is very common. You create a variable to hold a date by using the Date data type. You can get a date into a Date variable in several ways. Recall that when you set a string variable to a literal value, the literal is enclosed in quotes.