On to… string operations & functions
Concatenation (&) §When we want to combine two character strings into one new (longer) string, we can concatenate the strings: c = “23” d = “skidoo” e = c & d Picture1.Print e;”!” 23skidoo!
Relating characters & digits §Text boxes can contain only string data, so we must make use of special Visual Basic functions if we want to use this data in a numeric sense. l We know these... Val (“123”) returns the number 123 Str (123) returns the string “123” l These are new... Asc (“A”) returns the number 65 Chr (65) returns the string “A”
Categories of string functions §We have 3 categories of string functions: l Format functions l Those that return a string type result l Those that return a numeric type result Let’s see...
The Format Function §Used to format the display of numeric output data and dates: formatspecifier (num, decplaces) A numeric value or string representation of a numeric value or date; may be a variable An integer that specifies the desired number of decimal places; if omitted, the default value is 2
Examples FormatNumber ( ) FormatCurrency ( ) FormatNumber ( ,0) FormatPercent ( ,1) Format (12345,” ”) Format (12345,” ”) 12, $12, , % bbb12345 bbbb12345
Formatting Dates FormatDateTime(“2/5/01”, vbLongDate) Friday, February 5, 2001
String functions that return string results §These require different numbers of input values but they all return exactly one output value that is character string type: l Left (string, integer_value) l Right (string, integer_value) l Mid (string, integer_value, integer_value) l UCase (string) l Trim (string)
Examples Left (“abcdefg”, 4) returns “abcd” Right (“abcdefg”, 2) returns “fg” Mid (“abcdefg”, 3, 2) returns “cd” UCase (“abCd7f*”) returns “ABCD7F*” Trim (“ abcdefg ”) returns “abcdefg”
String functions that return numeric results §These each receive character string input values and return a unique numeric output value: Len (string) InStr (string1, string2)
Examples Len (“abcdefg”) returns 7 InStr (“abcdefg”, “de”) returns 4 Len (text1.text) returns the length of the input string
Lab §Page 133 # 130 (But input only one word, and replace one character in that word with another character) §Page 133 #131
Homework Due Next Tuesday §Page 135 # 138 §Page 133 # 130 (As stated in the book)