Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings in Visual Basic Words, Phrases, and Spaces.

Similar presentations


Presentation on theme: "Strings in Visual Basic Words, Phrases, and Spaces."— Presentation transcript:

1 Strings in Visual Basic Words, Phrases, and Spaces

2 Strings are a series of characters. ä Constant strings never change and are indicated by double quotes. ä Examples: “Fleeb” “Here is a string.”

3 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.

4 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.

5 Putting Strings Together ä & concatenates two strings ä Examples: ä “This “ & “is fun!” becomes “This is fun!” ä “Fleeb” & “norb” & “ski” becomes “Fleebnorbski”

6 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.

7 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”

8 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”

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”

10 Creating Strings ä Str$ - Creates a string from a number. ä CStr - Creates a string from a number. ä Format$ - Allows very complicated formatting of numbers.

11 Creating Strings ä Format$ - Used with numbers, gives precise formatting control over strings. ä Takes two arguments ä A number ä A descriptor.

12 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.

13 User Defined Format String Characters 0Display a number or zero. #Display a number or nothing..Decimal placeholder %Percentage placeholder.,Thousands seperator EScientific Notation :Time seperator /Date seperator \ ”Literal character indicator.

14 User Defined Format String Characters ä dDay ä mMonth ä yYear ä hHour ä nMinute ä sSecond ä wDay of Week ä qQuarter

15 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

16 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

17 More Examples format(cur_time,”dddd, mmm dd, yyyy”) Wednesday, Dec. 06, 1995

18 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.

19 Modifying Strings ä UCase$ - Make every letter upper case. ä Takes one argument, the string. ä Examples ä UCase$(“Here”) returns “HERE” ä UCase$(“Oh Boy”) returns “OH BOY”

20 Modifying Strings ä LCase$ - Makes every letter lower case. ä Takes one argument, the string. ä Examples ä LCase$(“Here”) returns “here” ä LCase$(“Oh Boy”) returns “oh boy”

21 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.

22 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”.

23 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.

24 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.

25 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

26 Summary ä String operations can be very useful. ä VB gives particularly strong support for strings.


Download ppt "Strings in Visual Basic Words, Phrases, and Spaces."

Similar presentations


Ads by Google