String Function

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View String Function as PDF for free.

More details

  • Words: 2,597
  • Pages: 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: Description: Syntax:

Len Returns a Long containing the length of the specified string Len(string)

Example:

Where string is the string whose length (number of characters) is to be returned. 1. lngLen = Len("Visual Basic") ' lngLen = 12

Function: Description: Syntax:

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:

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

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

Example:

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. 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: Description: Syntax: Example:

Function: Description: Syntax: Example:

Function: Description:

Syntax:

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

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

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.

Examples:

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

Function: Description:

Syntax:

(case-insensitive search)

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.

Examples:

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

Examples:

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

Function: Description:

Syntax:

1. strTest = Space$(5)

' strTest = "

"

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 = case-insensitive) Built-in "vb" constants can be used for the compare argument:

Examples:

vbBinaryCompare for 0 (case-sensitive search) vbTextCompare for 1 (case-insensitive search) 1. strNewDate = Replace$("08/31/2001", "/", "-") 2. ' strNewDate = "08-31-2001"

Function: Description:

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

Syntax: Examples:

Introduced in VB 6. StrReverse$(string) 1. strTest = StrReverse$("Visual Basic")

Function: Description: Syntax: Examples:

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

' strTest = "cisaBlausiV"

"Visual Basic "

Function: Description: Syntax: Examples:

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

Function: Description: Syntax: Examples:

Trim$ (or Trim) Removes both leading and trailing blank spaces from a string. Trim$(string) 1. strTest = Trim$(" Visual Basic ") ' strTest = "Visual Basic" 2. ' 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) 1. intCode = Asc("*") ' intCode = 42 2. intCode = Asc("ABC")

' intCode = 65

Function: Description: Syntax:

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

Examples:

Where charcode is a number from 0 to 255 that identifies the character. 1. strChar = Chr$(65) ' strChar = "A"

ASCII Character Codes (0 through 127) 0 1

N/A N/A

32 33

[space] !

64 65

@ A

96 97

` a

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 (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

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

" # $ % & ' ( ) * + , . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ?

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

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 [ \ ] ^ _

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

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

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

à á â ã ä å æ ç è é ê ë ì à î ï ð ñ ò ó ô

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

ASCII Character Codes (128 through 255) 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

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

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

[space] ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬  ® ¯ ° ± ² ³ ´

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

À Á Â Ã Ä Ã… Æ Ç È É Ê Ë ÃŒ Ãヘ ÃŽ Ï Ð Ñ Ã’ Ó Ô

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

181 182 183 184 185 186 187 188 189 190 191

µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿

213 214 215 216 217 218 219 220 221 222 223

å Ö × Ø ê Ú Û Ü Ý Þ ß

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

Related Documents

String Function
May 2020 9
Sql String Function
October 2019 1
String
June 2020 26
Function
December 2019 67
Function
November 2019 54
Function
June 2020 25