Sunteți pe pagina 1din 8

Originally Written By TheVBProgramer. Cleaned up and reformatted for this site.

NOTE: This tutorial uses The VB Test Harness. If you have not already created this test harness please do so first. It will make this tutorial a lot easier to
follow.

VB has numerous built-in string functions for processing strings. Most VB string-handling functions return a string, although some return a number (such as the Len
function, which returns the length of a string and functions like Instr and InstrRev, which return a character position within the string). The functions that return
strings can be coded with or without the dollar sign ($) at the end, although it is more efficient to use the version with the dollar sign.

The first time I started trying to understand the VB6 string functions I was somewhat confused. This tutorial will walk you through all the different ways you can us
VB to handle strings. If you are still confused feel free to post a comment and hopefully we can help get you cleared up. Also there are many other string related
tutorials on this site so feel free to browse around.

Function: Len
Description: Returns a Long containing the length of the specified string
Syntax: Len(string)

Where string is the string whose length (number of characters) is to be returned.


Example: 1. lngLen = Len("Visual Basic") ' lngLen = 12

Function: Mid$ (or Mid)


Description: Returns a substring containing a specified number of characters from a string.
Syntax: Mid$(string, start[, length])

The Mid$ function syntax has these parts:

string Required. String expression from which characters are returned.

start Required; Long. Character position in string at which the part to be taken begins. If start is
greater than the number of characters in string, Mid returns a zero-length string ("").

length Optional; Long. Number of characters to return. If omitted or if there are fewer than
length characters in the text (including the character at start), all characters from the start
position to the end of the string are returned.

Example: 1. strSubstr = Mid$("Visual Basic", 3, 4) ' strSubstr = "sual"

Note: Mid$ can also be used on the left side of an assignment statement, where you can replace
a substring within a string.

1. strTest = "Visual Basic"


2. Mid$(strTest, 3, 4) = "xxxx"
3.
4. 'strTest now contains "Vixxxx Basic"

In VB6, the Replace$ function was introduced, which can also be used to replace characters
within a string.

Function: Left$ (or Left)


Description: Returns a substring containing a specified number of characters from the beginning (left side) of
a string.
Syntax: Left$(string, length)

The Left$ function syntax has these parts:

string Required. String expression from which the leftmost characters are returned.

length Required; Long. Numeric expression indicating how many characters to return. If 0, a
zero-length string ("") is returned. If greater than or equal to the number of characters in string,
the entire string is returned.

Example: 1. strSubstr = Left$("Visual Basic", 3) ' strSubstr = "Vis"


2.
3. ' Note that the same thing could be accomplished with Mid$:

4. strSubstr = Mid$("Visual Basic", 1, 3)

Function: Right$ (or Right)


Description: Returns a substring containing a specified number of characters from the end (right side) of a
string.
Syntax: Right$(string, length)

The Right$ function syntax has these parts:

string Required. String expression from which the rightmost characters are returned.

length Required; Long. Numeric expression indicating how many characters to return. If 0, a
zero-length string ("") is returned. If greater than or equal to the number of characters in string,
the entire string is returned.
Example: 1. strSubstr = Right$("Visual Basic", 3) ' strSubstr = "sic"
2.
3. ' Note that the same thing could be accomplished with Mid$:

4. strSubstr = Mid$("Visual Basic", 10, 3)

Function: UCase$ (or UCase)


Description: Converts all lowercase letters in a string to uppercase. Any existing uppercase letters and non-
alpha characters remain unchanged.
Syntax: UCase$(string)
Example: 1. strNew = UCase$("Visual Basic") ' strNew = "VISUAL BASIC"

Function: LCase$ (or LCase)


Description: Converts all uppercase letters in a string to lowercase. Any existing lowercase letters and non-
alpha characters remain unchanged.
Syntax: LCase$(string)
Example: 1. strNew = LCase$("Visual Basic") ' strNew = "visual basic"

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])

The InStr function syntax has these parts:

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.

string1 Required. String expression being searched.


string2 Required. String expression sought.

compare Optional; numeric. A value of 0 (the default) specifies a binary (case-sensitive) search.
A value of 1 specifies a textual (case-insensitive) search.
Examples: 1. lngPos = Instr("Visual Basic", "a")
2. ' lngPos = 5
3.
4. lngPos = Instr(6, "Visual Basic", "a")
5. ' lngPos = 9 (starting at position 6)
6.
7. lngPos = Instr("Visual Basic", "A")
8. ' lngPos = 0 (case-sensitive search)
9.
10. lngPos = Instr(1, "Visual Basic", "A", 1)

11. ' lngPos = 5 (case-insensitive search)

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).

Introduced in VB 6.
Syntax: InStrRev(string1, string2[, start, [, compare]])

The InStr function syntax has these parts:

string1 Required. String expression being searched.

string2 Required. String expression sought.

start Optional. Numeric expression that sets the starting position for each search. If omitted,
search begins at the last character position.

compare Optional; numeric. A value of 0 (the default) specifies a binary (case-sensitive) search.
A value of 1 specifies a textual (case-insensitive) search.
Examples: 1. lngPos = InstrRev("Visual Basic", "a")
2. ' lngPos = 9
3.
4. lngPos = InstrRev("Visual Basic", "a", 6)
5. ' lngPos = 5 (starting at position 6)
6.
7. lngPos = InstrRev("Visual Basic", "A")
8. ' lngPos = 0 (case-sensitive search)
9.
10. lngPos = InstrRev("Visual Basic", "A", , 1)
11. ' lngPos = 9 (case-insensitive search)

12. ' Note that this last example leaves a placeholder for the start argument

Notes on Instr and InstrRev:

ℜ• 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:

vbBinaryCompare for 0 (case-sensitive search)


vbTextCompare for 1 (case-insensitive search)
Function: String$ (or String)
Description: Returns a string containing a repeating character string of the length specified.
Syntax: String$(number, character)

The String$ function syntax has these parts:

number Required; Long. Length of the returned string.

character Required; Variant. This argument can either be a number from 0 to 255 (representing
the ASCII character code* of the character to be repeated) or a string expression whose
first character is used to build the return string.
Examples: 1. strTest = String$(5, "a")
2. ' strTest = "aaaaa"
3.
4. strTest = String$(5, 97)

5. ' strTest = "aaaaa" (97 is the ASCII code for "a")

* A list of the ASCII character codes is presented at the end of this topic.

Function: Space$ (or Space)


Description: Returns a string containing the specified number of blank spaces.
Syntax: Space$(number)

Where number is the number of blank spaces desired.

Examples: 1. strTest = Space$(5) ' strTest = " "

Function: Replace$ (or Replace)


Description: Returns a string in which a specified substring has been replaced with another substring a
specified number of times.

Introduced in VB 6.
Syntax: Replace$(expression, find, replacewith[, start[, count[, compare]]])

The Replace$ function syntax has these parts:

expression Required. String expression containing substring to replace.

find Required. Substring being searched for.

replacewith Required. Replacement substring.

start Optional. Position within expression where substring search is to begin. If omitted, 1 is
assumed.

count Optional. Number of substring substitutions to perform. If omitted, the default value is
–1, which means make all possible substitutions.

compare Optional. Numeric value indicating the kind of comparison to use when evaluating
substrings. (0 = case sensitive, 1 = case-insensitive)

Built-in "vb" constants can be used for the compare argument:

vbBinaryCompare for 0 (case-sensitive search)


vbTextCompare for 1 (case-insensitive search)
Examples: 1. strNewDate = Replace$("08/31/2001", "/", "-")

2. ' strNewDate = "08-31-2001"


Function: StrReverse$ (or StrReverse)
Description: Returns a string in which the character order of a specified string is reversed.

Introduced in VB 6.
Syntax: StrReverse$(string)
Examples: 1. strTest = StrReverse$("Visual Basic") ' strTest = "cisaBlausiV"

Function: LTrim$ (or LTrim)


Description: Removes leading blank spaces from a string.
Syntax: LTrim$(string)
Examples: 1. strTest = LTrim$(" Visual Basic ")

2. ' strTest = "Visual Basic "

Function: RTrim$ (or RTrim)


Description: Removes trailing blank spaces from a string.
Syntax: RTrim$(string)
Examples: 1. strTest = RTrim$("Visual Basic") ' strTest = "Visual Basic"

Function: Trim$ (or Trim)


Description: Removes both leading and trailing blank spaces from a string.
Syntax: Trim$(string)
Examples: 1. strTest = Trim$(" Visual Basic ") ' strTest = "Visual Basic"

2. ' Note: Trim$(x) accomplishes the same thing as LTrim$(RTrim$(x))

Function: Asc
Description: Returns an Integer representing the ASCII character code corresponding to the first letter in a
string.
Syntax: Asc(string)
Examples: 1. intCode = Asc("*") ' intCode = 42

2. intCode = Asc("ABC") ' intCode = 65

Function: Chr$ (or Chr)


Description: Returns a string containing the character associated with the specified character code.
Syntax: Chr$(charcode)

Where charcode is a number from 0 to 255 that identifies the character.


Examples: 1. strChar = Chr$(65) ' strChar = "A"

ASCII Character Codes (0 through 127)

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

N/A = These characters aren't supported by Microsoft Windows.

ASCII Character Codes (128 through 255)

128 N/A 160 [space] 192 À 224 Ã


129 N/A 161 ¡ 193 Á 225 á
130 N/A 162 ¢ 194 Â 226 â
131 N/A 163 £ 195 Ã 227 ã
132 N/A 164 ¤ 196 Ä 228 ä
133 N/A 165 ¥ 197 Å 229 å
134 N/A 166 ¦ 198 Æ 230 æ
135 N/A 167 § 199 Ç 231 ç
136 N/A 168 ¨ 200 È 232 è
137 N/A 169 © 201 É 233 é
138 N/A 170 ª 202 Ê 234 ê
139 N/A 171 « 203 Ë 235 ë
140 N/A 172 ¬ 204 Ì 236 ì
141 N/A 173 Â 205 Ãヘ 237 Ã
142 N/A 174 ® 206 Î 238 î
143 N/A 175 ¯ 207 Ï 239 ï
144 N/A 176 ° 208 Ð 240 ð
145 N/A 177 ± 209 Ñ 241 ñ
146 N/A 178 ² 210 Ò 242 ò
147 N/A 179 ³ 211 Ó 243 ó
148 N/A 180 ´ 212 Ô 244 ô
149 N/A 181 µ 213 å 245 õ
150 N/A 182 ¶ 214 Ö 246 ö
151 N/A 183 · 215 × 247 ÷
152 N/A 184 ¸ 216 Ø 248 ø
153 N/A 185 ¹ 217 ê 249 ù
154 N/A 186 º 218 Ú 250 ú
155 N/A 187 » 219 Û 251 û
156 N/A 188 ¼ 220 Ü 252 ü
157 N/A 189 ½ 221 Ý 253 ý
158 N/A 190 ¾ 222 Þ 254 þ
159 N/A 191 ¿ 223 ß 255 ÿ

N/A = These characters aren't supported by Microsoft Windows.

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.

"Try It" Example

To demonstrate the built-in string functions, set up a "Try It" project, and place the following code in the cmdTryIt_Click event:

1. Private Sub cmdTryIt_Click()


2. Dim strTest As String
3.
4. strTest = InputBox("Please enter a string:")
5.
6. Print "Using Len:"; Tab(25); Len(strTest)
7. Print "Using Mid$:"; Tab(25); Mid$(strTest, 3, 4)
8. Print "Using Left$:"; Tab(25); Left$(strTest, 3)
9. Print "Using Right$:"; Tab(25); Right$(strTest, 2)
10. Print "Using UCase$:"; Tab(25); UCase$(strTest)
11. Print "Using LCase$:"; Tab(25); LCase$(strTest)
12. Print "Using Instr:"; Tab(25); InStr(strTest, "a")
13. Print "Using InstrRev:"; Tab(25); InStrRev(strTest, "a")
14. Print "Using LTrim$:"; Tab(25); LTrim$(strTest)
15. Print "Using RTrim$:"; Tab(25); RTrim$(strTest)
16. Print "Using Trim$:"; Tab(25); Trim$(strTest)
17. Print "Using String$ & Space$:"; Tab(25); String$(3, "*") _
18. & Space$(2) _
19. & Trim$(strTest) _
20. & Space$(2) _
21. & String$(3, 42)
22. Print "Using Replace$:"; Tab(25); Replace$(strTest, "a", "*")
23. Print "Using StrReverse$:"; Tab(25); StrReverse$(strTest)
24. Print "Using Asc:"; Tab(25); Asc(strTest)
25. End Sub

Run the project and click the "Try It" button. When the input box comes up, enter a string of your choice.

Some tips on what to enter:

• 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:
Download the VB project code for the example above here.

S-ar putea să vă placă și