Você está na página 1de 9

Function: Description: Syntax:

Len Returns a Long containing the length of the specified string Len(string) Where string is the string whose length (number of characters) is to be returned.

Example: Function: Description: Syntax:

lngLen = Len("Visual Basic")

' lngLen = 12

Mid$ (or Mid) Returns a substring containing a specified number of characters from a string. 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:

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

' strSubstr =

Note: Mid$ can also be used on the left side of an assignment statement, where you can replace a substring within a string.
strTest = "Visual Basic" Mid$(strTest, 3, 4) = "xxxx" 'strTest now contains "Vixxxx Basic"

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

Function: Description: Syntax:

Left$ (or Left) Returns a substring containing a specified number of characters from the beginning (left side) of a string. 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:

strSubstr = Left$("Visual Basic", 3)

' strSubstr = "Vis"

' Note that the same thing could be accomplished with Mid$: strSubstr = Mid$("Visual Basic", 1, 3)

Function: Description: Syntax:

Right$ (or Right) Returns a substring containing a specified number of characters from the end (right side) of a string. 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:

strSubstr = Right$("Visual Basic", 3)

' strSubstr = "sic"

' Note that the same thing could be accomplished with Mid$: strSubstr = Mid$("Visual Basic", 10, 3)

Function: Description: Syntax: Example: Function: Description: Syntax: Example:

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

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

Function: Description:

Syntax:

Instr 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). 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 (casesensitive) search. A value of 1 specifies a textual (case-insensitive) search.

Examples:

lngPos = Instr("Visual Basic", "a") ' lngPos = 5 lngPos = Instr(6, "Visual Basic", "a") ' lngPos = 9 (starting at position 6) lngPos = Instr("Visual Basic", "A") ' lngPos = 0 (case-sensitive search) lngPos = Instr(1, "Visual Basic", "A", 1) ' lngPos = 5 (case-insensitive search)

Function: Description:

InstrRev 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. 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 (casesensitive) search. A value of 1 specifies a textual (case-insensitive) search.

Syntax:

Examples:

lngPos = InstrRev("Visual Basic", "a") ' lngPos = 9 lngPos = InstrRev("Visual Basic", "a", 6) ' lngPos = 5 (starting at position 6) lngPos = ' lngPos = 0 InstrRev("Visual Basic", "A") (case-sensitive search)

lngPos = InstrRev("Visual Basic", "A", , 1) ' lngPos = 9 (case-insensitive search) ' Note that this last example leaves a placeholder for the start argument

Notes on Instr and InstrRev: y 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: Description: Syntax:

String$ (or String) Returns a string containing a repeating character string of the length specified. 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:

strTest = String$(5, "a") ' strTest = "aaaaa" strTest = String$(5, 97) ' 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: Description: Syntax: Space$ (or Space) Returns a string containing the specified number of blank spaces. Space$(number) Where number is the number of blank spaces desired. Examples:

strTest = Space$(5)

' strTest = "

"

Function: Description:

Replace$ (or Replace) Returns a string in which a specified substring has been replaced with another substring a specified number of times. Introduced in VB 6. 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 = caseinsensitive) Built-in "vb" constants can be used for the compare argument: vbBinaryCompare for 0 (case-sensitive search) vbTextCompare for 1 (case-insensitive search)

Syntax:

Examples:

strNewDate = Replace$("08/31/2001", "/", "-") ' strNewDate = "08-31-2001"

Function: Description:

StrReverse$ (or StrReverse) Returns a string in which the character order of a specified string is reversed. Introduced in VB 6. StrReverse$(string)
strTest = StrReverse$("Visual Basic") "cisaBlausiV" ' strTest =

Syntax: Examples:

Function: Description: Syntax: Examples:

LTrim$ (or LTrim) Removes leading blank spaces from a string. LTrim$(string)
strTest = ' strTest = LTrim$(" Visual Basic "Visual Basic " ")

Function: Description: Syntax: Examples:

RTrim$ (or RTrim) Removes trailing blank spaces from a string. RTrim$(string)
strTest = RTrim$("Visual Basic") Basic" ' strTest = "Visual

Function: Description: Syntax: Examples:

Trim$ (or Trim) Removes both leading and trailing blank spaces from a string. Trim$(string)
strTest = Trim$(" Visual Basic ") ' strTest = "Visual Basic" ' Note: Trim$(x) accomplishes the same thing as LTrim$(RTrim$(x))

Function: Description: Syntax: Examples:

Asc Returns an Integer representing the ASCII character code corresponding to the first letter in a string. Asc(string)
intCode = Asc("*") intCode = Asc("ABC") ' intCode = 42 ' intCode = 65

Function: Description: Syntax:

Chr$ (or Chr) Returns a string containing the character associated with the specified character code. Chr$(charcode) Where charcode is a number from 0 to 255 that identifies the character.

Examples:

strChar = Chr$(65)

' strChar = "A"

ASCII Character Codes (0 through 127) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 N/A N/A N/A N/A N/A N/A N/A N/A (backspace) (tab) (line feed) N/A N/A (carriage return) N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 [space] ! " # $ % & ' ( ) * + , . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 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.

Você também pode gostar