19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Like the numeric functions discussed before, Left, Mid, Right, UCase, <strong>and</strong> Trim also can<br />

be evaluated for variables <strong>and</strong> expressions.<br />

EXAMPLE 5<br />

The following program evaluates the functions above for variables <strong>and</strong> expressions. Note that spaces are<br />

counted as characters.<br />

Private Sub cmdEvaluate_Click()<br />

Dim str1 As String, str2 As String<br />

‘Evaluate functions at variables <strong>and</strong> expressions.<br />

picResults.Cls<br />

str1 = “Quick as ”<br />

str2 = “a wink”<br />

picResults.Print Left(str1, 7)<br />

picResults.Print Mid(str1 & str2, 7, 6)<br />

picResults.Print UCase(str1 & str2)<br />

picResults.Print “The average ”; Right(str2, 4); “ lasts .1 second.”<br />

picResults.Print Trim(str1); str2<br />

End Sub<br />

[Run <strong>and</strong> then click the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

Quick a<br />

as a w<br />

QUICK AS A WINK<br />

The average wink lasts .1 second.<br />

Quick asa wink<br />

■ STRING-RELATED NUMERIC FUNCTIONS: LEN, INSTR<br />

The functions Len <strong>and</strong> InStr operate on strings but produce numbers. The function Len gives<br />

the number of characters in a string. The function InStr searches for the first occurrence of one<br />

string in another <strong>and</strong> gives the position at which the string is found. Suppose str1 <strong>and</strong> str2 are<br />

strings. The value of Len(str1) is the number of characters in str1. The value of InStr(str1, str2)<br />

is 0 if str2 is not a substring of str1. Otherwise, its value is the first position of str2 in str1.<br />

Some examples of Len <strong>and</strong> InStr follow:<br />

Len(“Shen<strong>and</strong>oah”) is 10. InStr(“Shen<strong>and</strong>oah”, “n<strong>and</strong>”) is 4.<br />

Len(“Just a moment”) is 13. InStr(“Just a moment”, “ ”) is 5.<br />

Len(“ ”) is 1. InStr(“Croissant”, “ist”) is 0.<br />

EXAMPLE 6<br />

The following program evaluates functions at variables <strong>and</strong> expressions. The ninth line locates the position<br />

of the space separating the two names. The first name will end one position to the left of this position<br />

<strong>and</strong> the last name will consist of all but the first n characters of the full name.<br />

Private Sub cmdAnalyze_Click()<br />

Dim nom As String ‘Name<br />

Dim n As Integer ‘Location of space<br />

Dim first As String ‘First name<br />

Dim last As String ‘Last name ‘<br />

Evaluate functions at variables <strong>and</strong> expressions.<br />

picResults.Cls<br />

nom = txtFullName.Text<br />

n = InStr(nom, “ ”)<br />

first = Left(nom, n - 1)<br />

Built-In Functions 61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!