Chapter 6
Part B

String Conversion Functions

As you have seen, when you want to compare a user  input with a specific string, you can encounter a problem. You may have solved this by using a complex Boolean:

If ((strUserAnswer = "Y") Or (strUserAnswer = "y')) Then  'allow for upper OR lower case

Visual Basic provides functions that can convert a string to either all uppercase or all lowercase. The LCase( ) and UCase( ) accomplish this. Thus, to enact the If statement above, you can use:

If ((UCase(strUserAnswer) = "Y") Then  'also allows for either case- by converting to upper case

or

If ((LCase(strUserAnswer) = "y") Then  'also allows for either case- by converting to lower case

Manipulating Strings

Strings can be taken apart and put back together by using the build-in Left, Right, and Mid functions.

The Left function takes 2 arguments, a string and a number. It returns a string with the length of the number beginning at the left:

MsgBox Left("The Rain in Spain", 8)

produces:

The Right function also takes 2 arguments, a string and a number. It returns a string with the length of the number beginning at the right:

MsgBox Right("The Rain in Spain", 8)

produces

The Mid function has either 2 or three parameters (the third one is optional). With 2 parameters (a string and an integer, it returns the string beginning at the position of the integer:

MsgBox Mid("The Rain in Spain", 5)

produces

With a third (integer) parameter, it returns the string that begins at the position of the first integer and has a length of the second.

MsgBox Mid("The Rain in Spain", 5, 4)

produces

To be compatible with older versions of Basic, any of these command can be written with a "$" (Left$, Right$, Mid$). Don't do this!

Command

Format

Example

Left( ), Left$( )

Left[$](strexpr, n)

Left("Ray Cafolla", 3) = "Ray"

Right( ), Right$( )

Right[$](strexpr, n)

Right("Ray Cafolla, 7) = "Cafolla

Mid( ), Mid$( ) Mid[$](stringexpr, start[, length]) Mid("Ray Cafolla",4,3) = "Caf"

The Len( ) function takes a single string arguement and returns the length of the string.

MsgBox Len("The Rain in Spain")

Produces

The InStr( ) is one of the more complex and powerful of the string manipulation commands. It also can have either 2 or 3 parameters, but it it the first one that is optional. Let's take the easiest one first- it takes 2 arguments, both strings. It returns the integer representing the position in the first string the second string starts at. If the second string does not occur in the first string, a zero is returned.

MsgBox InStr("The rain in Spain", "Spain")

produces

If you add an integer as an optional first parameter, the search for the string begins at the given position. Thus (with 2 parameters)

 MsgBox InStr("The rain in Spain", "in")

Produces

while

 MsgBox InStr(8, "The rain in Spain", "in")

Produces

Len( )

Len(strexpr)

Len("Ray Cafolla") = 11

InStr( )

InStr(strexpr1, strexpr2 )

InStr(start, strexpr1, strexpr2)

Instr("Ray Cafolla", "a") = 2

Instr("Ray Cafolla", "xyz") = 0

Instr(14 "Ray Cafolla, "a", 0) = 6

Instr(1, "Ray Cafolla, "c", 0) = 0

The For...Next Loop

Recall that loops can either be definite (meaning the program "knows" how many times to loop) or indefinite (meaning it is executed until some condition is met). One of the most common errors in definite loops is the "off by one" error. This can be avoided by using a special loop structure called the For..Next Loop. It looks like this:

For intCount = 1 to 10 Step 1

do something
do something
do something

Next intCount

The commands in this loop are executed exactly 10 times. Use For..Next loops when you need a definite loop!

Using ASCII Codes

The ASC( ) function takes a string argument and returns the ASCII equivalent. 

MsgBox ASC("A")

produces the number 65

The complementary Chr( ) function takes an integer argument and returns the string character.

MsgBox Chr(65)

produces the string "A"

Homework

The Palindrome Problem
Chapter 6 Exercises 7, 8, 9, 10, 11