VB String-Handling Functions

 

VB has numerous built-in 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.

 

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:

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:

 

Part      Description

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

 

               Example:                   

 

       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:

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:

 

Part      Description

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:

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:

 

Part      Description

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:

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:

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:

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:

 

Part       Description

 

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:

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:

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:

 

Part       Description

 

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:

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

 

 

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:

 

Part                  Description

 

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:

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:

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:

 

Part                Description

 

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:

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

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

strTest = StrReverse$("Visual Basic")           ' strTest = "cisaB lausiV"

 

 

 

Function:

LTrim$ (or LTrim)

 

Description:

Removes leading blank spaces from a string.

 

Syntax:

LTrim$(string)

 

Examples:

strTest = LTrim$("  Visual Basic  ")

' strTest = "Visual Basic  "

 

 

 

Function:

RTrim$ (or RTrim)

Description:

Removes trailing blank spaces from a string.

 

Syntax:

RTrim$(string)

 

Examples:

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:

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 the first letter in a string.

 

Syntax:

Asc(string)

 

Examples:

intCode = Asc("*")      ' intCode = 42

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:

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:

 

Private Sub cmdTryIt_Click()

 

    Dim strTest    As String

 

    strTest = InputBox("Please enter a string:")

 

    Print "Using Len:"; Tab(25); Len(strTest)

    Print "Using Mid$:"; Tab(25); Mid$(strTest, 3, 4)

    Print "Using Left$:"; Tab(25); Left$(strTest, 3)

    Print "Using Right$:"; Tab(25); Right$(strTest, 2)

    Print "Using UCase$:"; Tab(25); UCase$(strTest)

    Print "Using LCase$:"; Tab(25); LCase$(strTest)

    Print "Using Instr:"; Tab(25); InStr(strTest, "a")

    Print "Using InstrRev:"; Tab(25); InStrRev(strTest, "a")

    Print "Using LTrim$:"; Tab(25); LTrim$(strTest)

    Print "Using RTrim$:"; Tab(25); RTrim$(strTest)

    Print "Using Trim$:"; Tab(25); Trim$(strTest)

    Print "Using String$ & Space$:"; Tab(25); String$(3, "*") _

                                            & Space$(2) _

                                            & Trim$(strTest) _

                                            & Space$(2) _

                                            & String$(3, 42)

    Print "Using Replace$:"; Tab(25); Replace$(strTest, "a", "*")

    Print "Using StrReverse$:"; Tab(25); StrReverse$(strTest)

    Print "Using Asc:"; Tab(25); Asc(strTest)

 

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:

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.