Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

String Functions

Unlike the numeric functions, Visual Basic's string functions return a string and often work with one or more string arguments. Table 14.2 lists several of the more common string functions you'll work with. The string functions accept controls as well as variables, literals, and expressions.

Table 14.2. Common built-in string functions.

Function Description
Chr(int) Returns the ASCII character of the argument.
InStr([start,] str1, str2) Returns the starting position of str2 within str1, beginning at start if specified.
InstrRev(str1, str2 [,start]) Returns the starting position of str2 within str1 from the end of the string, beginning with start if specified.
Join(list [,delimeter]) Merges strings in the list, each separated by one space unless you specify a delimeter character.
LCase(str) Returns the argument in all lowercase letters.
Left(str, int) Returns the far-left int characters from the argument.
Len(str) Returns the number of characters in the string. (Notice that Len() works on numeric arguments as well.)
LTrim(str) Returns the string argument, without leading spaces.
Mid(str, intStart [, intLen]) Returns a substring of the argument, starting with the character at intStart and continuing until the entire the string is extracted or until the optional intLen characters have been extracted. Mid() is called the midstring function because it can return the middle portion of a string.
MonthName(month [,abbre]) With a month number, the month name is returned, abbreviated to 3 characters if you specify True for abbrev.
Right(str, int) Returns the far-right int characters from the argument.
RTrim(str) Returns the string argument, without trailing spaces.
Str() Converts its numeric argument to a string with the numeric digits in the string.
StrReverse(string) Returns the string argument that is completely reversed.
UCase(str) Returns the argument in all uppercase letters.
WeekdayName(weekday [, abbrev]) Returns the day of the week as a string given the numeric weekday argument, and abbreviated if abbrev is True

A substring is part of a string.

Suppose you want to determine whether a string variable's value will fit inside a text box before you attempt to assign the string to the Text Box control (assume that the text box doesn't have AutoSize set to True). If the text box is large enough to hold 20 characters, the following If statement fragment will be True if the string fits in the text box:

If (Len(strVar) <= 20) Then    'String fits

Suppose you need to compare two password string values. Given that the user may have entered the password in all uppercase or a case mixture, the following code tests the stored password against one entered in a string variable, and the code uses UCase() to ensure that they compare with the same case matches:

If UCase(strUser) = UCase(strPassword) Then
' This If leg is true if the passwords match

The LTrim() function is often useful for trimming the leading blank from strings you make from numbers. For example, Str(123) returns the string literal " 123" (notice the leading blank). Sometimes, when writing certain kinds of files, you need to write strings of data instead of numbers and Str() comes in handy. If, however, you need to strip off the leading blank, you can embed Str() within LTrim() to return the string digits without the leading blank, like this: LTrim(Str(123)).

Left() returns the left part of a string or control value that Visual Basic converts to a string. Therefore, the following stores only the first five characters from the string argument into strAns:

strAns = Left(txtUser.Text, 5)

Whereas Left() returns the left part, Right() returns the right part of a string. Mid() can return the middle part of a string. Therefore, the following expression becomes "der" when Mid() returns the middle three letters: Mid("Federal", 3, 3). Because of the optional third argument, Mid() works like the Right() function if you omit the third argument because Mid() returns all characters from the starting position to the end of the string if you don't put the third argument inside Mid()'s argument list.

Share ThisShare This

Informit Network