Function VB

Embed Size (px)

Citation preview

  • 8/3/2019 Function VB

    1/9

    Function: LenDescription: Returns a Long containing the length of the specified stringSyntax: Len(string)

    Wherestringis the string whose length (number of characters) is to be

    returned.Example: 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:

    stringRequired. String expression from which characters are returned.

    startRequired; Long. Character position in string at which the part to be takenbegins. If start is greater than the number of characters in string, Midreturns a zero-length string ("").

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

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

    Note: Mid$ can also be used on the left side of an assignment statement, whereyou 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 toreplace characters within a string.

  • 8/3/2019 Function VB

    2/9

    Function: Left$ (orLeft)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:

    stringRequired. String expression from which the leftmost characters arereturned.

    length Required; Long. Numeric expression indicating how many characters toreturn. If 0, a zero-length string ("") is returned. If greater than or equal to thenumber 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: Right$ (orRight)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:

    stringRequired. String expression from which the rightmost characters arereturned.

    length Required; Long. Numeric expression indicating how many characters toreturn. If 0, a zero-length string ("") is returned. If greater than or equal to thenumber 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)

  • 8/3/2019 Function VB

    3/9

    Function: UCase$ (orUCase)Description: Converts all lowercase letters in a string to uppercase. Any existing uppercase

    letters and non-alpha characters remain unchanged.Syntax: UCase$(string)Example: strNew = UCase$("Visual Basic") ' strNew = "VISUAL

    BASIC"Function: LCase$ (orLCase)Description: Converts all uppercase letters in a string to lowercase. Any existing lowercase

    letters and non-alpha characters remain unchanged.Syntax: LCase$(string)Example: strNew = LCase$("Visual Basic") ' strNew = "visual

    basic"

    Function: Instr

    Description: Returns a Long specifying the position of one string within another. The searchstarts either at the first character position or at the position specified by thestartargument, and proceeds forward toward the end of the string (stoppingwhen eitherstring2 is found or when the end of thestring1 is reached).

    Syntax: InStr([start,] string1, string2 [, compare])

    The InStr function syntax has these parts:

    startOptional. Numeric expression that sets the starting position for eachsearch. If omitted, search begins at the firstcharacter position. The startargument 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: 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)

  • 8/3/2019 Function VB

    4/9

    Function: InstrRevDescription: 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 thestartargument, and proceeds backward toward the beginning of the string (stoppingwhen eitherstring2 is found or when the beginning of thestring1 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.

    startOptional. 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: lngPos = InstrRev("Visual Basic", "a")' lngPos = 9

    lngPos = InstrRev("Visual Basic", "a", 6)

    ' lngPos = 5 (starting at position 6)

    lngPos = InstrRev("Visual Basic", "A")

    ' lngPos = 0 (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

  • 8/3/2019 Function VB

    5/9

    Notes on Instr and InstrRev:

    y Something to watch out for is that while Instr and InstrRev both accomplish the samething (except that InstrRev processes a string from last character to first, while Instrprocesses 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 theInstrRev arguments are (string1, string2, start, compare).

    y The Instr function has been around since the earlier days of BASIC, whereas InstrRev wasnot introduced until VB 6.

    y 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$ (orString)Description: Returns a string containing a repeating character string of the length specified.Syntax: String$(number, character)

    The String$ function syntax has these parts:

    numberRequired; Long. Length of the returned string.

    characterRequired; Variant. This argument can either be a number from 0 to

    255 (representing the ASCII character code* of the character to berepeated) or a string expression whose first character is used to buildthe 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: Space$ (orSpace)Description: Returns a string containing the specified number of blank spaces.Syntax: Space$(number)

    Where numberis the number of blank spaces desired.

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

  • 8/3/2019 Function VB

    6/9

    Function: Replace$ (orReplace)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.

    findRequired. Substring being searched for.

    replacewith Required. Replacement substring.

    startOptional. Position within expression where substring search is to begin. I

    omitted, 1 is assumed.

    countOptional. Number of substring substitutions to perform. If omitted, thedefault value is 1, which means make all possiblesubstitutions.

    compare Optional. Numeric value indicating the kind of comparison to usewhen 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: strNewDate = Replace$("08/31/2001", "/", "-")' strNewDate = "08-31-2001"

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

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

    "cisaBlausiV"

  • 8/3/2019 Function VB

    7/9

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

    ' strTest = "Visual Basic "

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

    Basic"

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

    Basic"

    ' Note: Trim$(x) accomplishes the same thing as

    LTrim$(RTrim$(x))

    Function: Asc

    Description: Returns an Integer representing the ASCII character code corresponding to thefirst letter in a string.

    Syntax: Asc(string)Examples: intCode = Asc("*") ' intCode = 42

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

    Function: Chr$ (orChr)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: strChar = Chr$(65) ' strChar = "A"

  • 8/3/2019 Function VB

    8/9

    ASCIICharacterCodes(0through 127)

    0 N/A 32 [space] 64 @ 96 `1 N/A 33 ! 65 A 97 a2 N/A 34 " 66 B 98 b

    3 N/A 35 # 67 C 99 c4 N/A 36 $ 68 D 100 d5 N/A 37 % 69 E 101 e6 N/A 38 & 70 F 102 f7 N/A 39 ' 71 G 103 g8 (backspace) 40 ( 72 H 104 h9 (tab) 41 ) 73 I 105 i10 (line feed) 42 * 74 J 10611 N/A 43 + 75 K 107 k12 N/A 44 , 76 L 108 l

    13 (carriage return) 45 - 77 M 109 m14 N/A 46 . 78 N 110 n15 N/A 47 / 79 O 111 o16 N/A 48 0 80 P 112 p17 N/A 49 1 81 Q 113 q18 N/A 50 2 82 R 114 r19 N/A 51 3 83 S 115 s20 N/A 52 4 84 T 116 t21 N/A 53 5 85 U 117 u22 N/A 54 6 86 V 118 v

    23 N/A 55 7 87 W 119 w24 N/A 56 8 88 X 120 x25 N/A 57 9 89 Y 121 y26 N/A 58 : 90 Z 122 z27 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 supportedby Microsoft Windows.

    ASCIICharacterCodes(128 through 255)

    128 N/A 160 [space] 192 224

  • 8/3/2019 Function VB

    9/9

    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 supportedby Microsoft Windows.

    The values in the table are the Windows default. However, values in the ANSIcharacter set

    above 127 are determinedby the code page specific to youroperating system.