Visual Basic All Functions
Visual Basic All Functions
Function: Len
Description: Returns a Long containing the length of the specified string
Syntax: Len(string)
Part Description
Example: Note: Mid$ can also be used on the left side of an assignment statement, where
you can replace a substring within a string.
Example:
In VB6, the Replace$ function was introduced, which can also be used to
replace characters within a string.
Part Description
string Required. String expression from which the leftmost characters are
returned.
' Note that the same thing could be accomplished with Mid$:
Part Description
string Required. String expression from which the rightmost characters are
returned.
Example: strSubstr = Right$("Visual Basic", 3) ' strSubstr = "sic"
' Note that the same thing could be accomplished with Mid$:
Function: Instr
Description: Returns a Long specifying the position of one string within another. The search
starts either at the first character position or at the position specified by the
start argument, and proceeds forward toward the end of the string (stopping
when either string2 is found or when the end of the string1 is reached).
Syntax: InStr([start,] string1, string2 [, compare])
start Optional. Numeric expression that sets the starting position for
each search. If omitted, search begins at the first character position.
The start argument is required if compare is specified.
' lngPos = 5
Function: InstrRev
Description: Returns a Long specifying the position of one string within another. The search
starts either at the last character position or at the position specified by the
start argument, and proceeds backward toward the beginning of the string
(stopping when either string2 is found or when the beginning of the string1 is
reached).
Syntax: InStrRev(string1, string2[, start, [, compare]])
The InStr function syntax has these parts:
Part Description
start Optional. Numeric expression that sets the starting position for
each search. If omitted, search begins at the last character position.
Examples: lngPos = InstrRev("Visual Basic", "a")
' lngPos = 9
' Note that this last example leaves a placeholder for the start argument
Something to watch out for is that while Instr and InstrRev both accomplish the same
thing (except that InstrRev processes a string from last character to first, while Instr
processes a string from first character to last), the arguments to these functions are specified
in a different order. The Instr arguments are (start, string1, string2, compare) whereas the
InstrRev arguments are (string1, string2, start, compare).
The Instr function has been around since the earlier days of BASIC, whereas InstrRev
was not introduced until VB 6.
Built-in "vb" constants can be used for the compare argument:
Part Description
* A list of the ASCII character codes is presented at the end of this topic.
Part Description
Built-in "vb" constants can be used for the compare argument:
Examples: strNewDate = Replace$("08/31/2001", "/", "-")
Description: Returns a string in which the character order of a specified string is reversed.
Syntax: StrReverse$(string)
Examples: strTest = StrReverse$("Visual Basic") ' strTest = "cisaB lausiV"
Function: Asc
Description: Returns an Integer representing the ASCII character code corresponding to the
first letter in a string.
Syntax: Asc(string)
Examples: intCode = Asc("*") ' intCode = 42
0 N/A 32 [space] 64 @ 96 `
1 N/A 33 ! 65 A 97 a
2 N/A 34 " 66 B 98 b
3 N/A 35 # 67 C 99 c
4 N/A 36 $ 68 D 100 d
5 N/A 37 % 69 E 101 e
6 N/A 38 & 70 F 102 f
7 N/A 39 ' 71 G 103 g
8 (backspace) 40 ( 72 H 104 h
9 (tab) 41 ) 73 I 105 i
10 (line feed) 42 * 74 J 106 j
11 N/A 43 + 75 K 107 k
12 N/A 44 , 76 L 108 l
13 (carriage return) 45 - 77 M 109 m
14 N/A 46 . 78 N 110 n
15 N/A 47 / 79 O 111 o
16 N/A 48 0 80 P 112 p
17 N/A 49 1 81 Q 113 q
18 N/A 50 2 82 R 114 r
19 N/A 51 3 83 S 115 s
20 N/A 52 4 84 T 116 t
21 N/A 53 5 85 U 117 u
22 N/A 54 6 86 V 118 v
23 N/A 55 7 87 W 119 w
24 N/A 56 8 88 X 120 x
25 N/A 57 9 89 Y 121 y
26 N/A 58 : 90 Z 122 z
27 N/A 59 ; 91 [ 123 {
28 N/A 60 < 92 \ 124 |
29 N/A 61 = 93 ] 125 }
30 N/A 62 > 94 ^ 126 ~
31 N/A 63 ? 95 _ 127 N/A
The values in the table are the Windows default. However, values in the ANSI character set
above 127 are determined by the code page specific to your operating system.
To demonstrate the built-in string functions, set up a "Try It" project, and place the following
code in the cmdTryIt_Click event:
Print "Using Len:"; Tab(25); Len(strTest)
End Sub
Run the project and click the "Try It" button. When the input box comes up, enter a string of
your choice.
To see the effects of UCase$ and LCase$, enter a mixed case string.
To compare Instr and InstrRev, enter a string with at least two "a"s in it.
To see the effects of LTrim$, RTrim$, and Trim$, enter a string with leading and/or
trailing spaces.
To see the effect of Replace$, enter a string with at least one "a" in it.
You can also modify the code and run the project to see if you get the results you expect.
The screen shot below shows a run of the project using the code above where the string Visual
Basic was input: