Strings in Visual Basic Words, Phrases, and Spaces Strings are a series of characters. ä Constant strings never change and are indicated by double quotes. ä Examples: “Fleeb” “Here is a string.” Strings are a series of characters. ä Variable strings are a special type of variable. ä Example: ä Dim Fleeb as String In any of the following operators and functions you can use either a variable or constant string. Strings have their own operators and functions. Operators for putting strings together. ä Functions for pulling strings apart. ä Functions for creating strings. ä Functions for modifying strings. ä Operators for finding out things about strings. ä Putting Strings Together ä & concatenates two strings ä Examples: “This “ & “is fun!” becomes “This is fun!” ä “Fleeb” & “norb” & “ski” becomes “Fleebnorbski” ä Old Operator ä The + sign can be used for the same purpose, but it is better to use the & since no one will mistake it for addition. Functions for Pulling Strings Apart ä Left$ - Pulls off the leftmost characters. ä Takes two arguments, the string ä the number of characters to pull off ä ä Examples: Left$(“Here is another string”,10) returns “Here is an” ä Left$(“6 + 67 * 9”,6) returns “6 + 67” ä Pulling Strings Apart ä Right$ - Pulls off the rightmost characters. ä Takes two arguments: the string ä the number of characters to pull off ä ä Examples: Right$(“Here is another string”,10) returns “her string” ä Right$(“6 + 67 * 9”,6) returns “67 * 9” ä Pulling Strings Apart ä Mid$ - Pulls out characters from middle. ä Takes three arguments the string ä the position to start pulling from. ä the number of characters to pull. ä ä Examples: Mid$(“Here is another string”,10,4) returns “noth” ä Mid$(“6 + 67 * 9”,5,2) returns “67” ä Creating Strings Str$ - Creates a string from a number. ä CStr - Creates a string from a number. ä Format$ - Allows very complicated formatting of numbers. ä Creating Strings ä Format$ - Used with numbers, gives precise formatting control over strings. ä Takes two arguments A number ä A descriptor. ä Descriptors for Format$ ä Descriptors come in two flavors: system defined and user defined. System defined include: General Number, Currency, Fixed, Standard, Percent, Scientific, Yes/No, True/False, On/Off ä User defined are generated using another string. The other string has special characters in it that define how the number should be displayed. ä User Defined Format String Characters 0 # . % , E : / \ ” Display a number or zero. Display a number or nothing. Decimal placeholder Percentage placeholder. Thousands seperator Scientific Notation Time seperator Date seperator Literal character indicator. User Defined Format String Characters ä ä ä ä ä ä ä ä d m y h n s w q Day Month Year Hour Minute Second Day of Week Quarter Some Examples of Format format(10201.2,”000000.00”) -> 010201.20 ä format(11.1,”####.##”) -> 11.1 ä format(10010021,”###,###,###”) -> 10,010,021 ä format(1000000,”#.##E##”) -> 1.00E6 ä Some Examples of Format Using Time Formats Dim curr_time as double curr_time = Now format(cur_time,”mm/dd/yy hh:nn”) -> 03/06/95 09:15 format(cur_time,”m/d/yy h:n”) -> 3/6/95 9:15 More Examples format(cur_time,”dddd, mmm dd, yyyy”) Wednesday, Dec. 06, 1995 Functions for Modifying Strings UCase$ - Make every letter upper case. ä LCase$ - Make every letter lower case. ä Trim$ - Removes leading and trailing spaces from the string. ä Modifying Strings ä UCase$ - Make every letter upper case. ä Takes one argument, the string. ä Examples UCase$(“Here”) returns “HERE” ä UCase$(“Oh Boy”) returns “OH BOY” ä Modifying Strings ä LCase$ - Makes every letter lower case. ä Takes one argument, the string. ä Examples LCase$(“Here”) returns “here” ä LCase$(“Oh Boy”) returns “oh boy” ä Modifying Strings ä Trim$ - Removes leading and trailing spaces from string ä Takes one argument, the string ä Examples Trim$(“ Here is a string returns “Here is a string” ä “) Also comes in LTrim$ and RTrim$, for removing only one end or the other. Functions for Finding Things Out About Strings Len - Finds out how long the string is. ä InStr - Finding out if one string is in another. ä StrComp - Find out which string is “bigger”. ä Finding Out about Strings ä Len - Finds out how long the string is. ä Takes ä ä one argument A string Returns the number of characters in the string. Finding Out about Strings ä InStr - Finding out if one string is in another. ä Takes two arguments The string to look in. ä The string to look for. ä ä Returns the location of the first occurance of string two in string one. ä If the location equals zero, there is no occurrence of string one in string two. Finding Out about Strings ä StrComp - Which string is “bigger”. ä Takes two arguments: string one ä string two ä ä Returns: -1 if string one < string two ä 0 if they are equal ä 1 if string two < string one ä Summary String operations can be very useful. ä VB gives particularly strong support for strings. ä