1 / 28

Strings

Strings. Chapter 6. String Conversion Functions. LCase – Built-in functions for converting strings into lowercase string. Example: lblDisplay.Caption = LCase(string) ‘returns the string in lowercase letters. UCase – returns the string in all uppercase. UCase(string). StrConv.

elie
Télécharger la présentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings Chapter 6

  2. String Conversion Functions • LCase – Built-in functions for converting strings into lowercase string. • Example: lblDisplay.Caption = LCase(string) ‘returns the string in lowercase letters. • UCase – returns the string in all uppercase. UCase(string)

  3. StrConv • Converts the string how the programmer wants. • Example: StrConv(string, conversion) • String represents what is being converted. • Conversion – determines how the string will be converted. • vbUpperCase • vbLowerCase • vbProperCase

  4. Example of StrConv Dim strTitle As String strTitle = “summer fun” lblBook.Caption = StrConv(strTitle,vbProperCase) This will display “Summer Fun” to the label

  5. Manipulating Strings Visual Basic includes several built-in functions for manipulating strings: • Left(string, length) • Right(string, length) • Mid(string, start, length) • Mid(string1, start, length) = string2 ‘Statement not a function

  6. Left(string, length) function • String – represents a string variable (word use to represent text) or string literal (text enclosed in double quotes) • Length represents the number of characters it will return. • Left(string, length) – returns the number of characters (length) starting at the left of the text.

  7. Example of Left(string, length) Dim strSeason As String strSeason = “Summer” lblDisplaySeason.Caption = Left(strSeason, 3)‘Sum will be display

  8. Right(string,length) • String – represents a string variable (word use to represent text) or string literal (text enclosed in double quotes) • Length represents the number of characters it will return. • Right(string, length) – returns the number of characters (length) starting at the right of the text. String – represents a string variable (word use to represent text) or string literal (text enclosed in double quotes)

  9. Mid(string, start, length) Mid returns the length number of characters starting with the start character of string. Example: Dim strSeason As String strSeason= “summer” lblDisplay.Caption = Mid(strSeason, 2,4) ‘umme is display

  10. Mid Statement Similar to the Mid function. Used to replace a specified number of characters in a string. Form: Mid(string1, start, length) = string2

  11. Example of Mid Statement Dim strSeason As String strSeason =“autumn” Mid(strSeason,2,3) = “summer” lblDisplay.Caption = strSeason ‘utu is replaced by sum ‘asummn

  12. The Len Function • Is a Visual Basic built-in function that returns a count of the number of characters in a string. • Form: Len(string)‘ where string is a string variable or literal string.

  13. Example of Len(string) Dim strSeason As String strSeason = “Summer” lblDisplayLength.Caption = Len(strSeason) ‘6 will be displayed

  14. The InStr Function Returns the starting position of a substring (part of a string). If the substring, is not found 0 is return Form: InStr(start, string1, string2) • Start is the position in string1 to start searching. If start is not included, searching automatically starts at position 1. • String1 is a String variable or string enclosed in quotation marks that will be search. • String2 is the substring being searched for.

  15. Example InStr(start, string1, string2) Dim strSeason As String, strSubString As String strSeason = “Summer” strSubString = “um” lblDisplay.Caption = InStr(strSeason, strSubString) ‘ 2 is displayed

  16. Generating Strings • String function – built-in function that returns a string of repeating characters. • String function form: String(integer, character) • Integer represents the number of characters the String function returns • Character is the character that is being returned.

  17. Space function • Built-in function that returns a string of spaces • Form: Space(integer) • Integer represents the number of spaces in the returned string.

  18. & operator • Concatenation of strings – process of joining two or more strings into one string • Form: string = string1 & string2 • String will contain string1 and string2 combined

  19. Example of the & operator strFirst = “Bob” strLast = “Smith” strFullName = strFirst & strLast ‘strFullName has BobSmith in it

  20. Two built-in constants vbTab – represents the tab character which represents 8 spaces in a string vbCrLf – represents carriage return MsgBox “hello” & vbTab & “and” & vbCrLf & “Good-bye” ‘Displays hello and good-bye

  21. Character Data Storage • Unicode – created by the ISO (International Standards Org.) uses two bytes to represent a character. Ranges from 0-65,535. Represents just about every character & symbol of every language. Data stores in bytes (8 bits) • Characters are stored as binary numbers. • ANSI – American Standard Institute developed ASCII • ASCII – (American Standard Code for Information Interchange) uses numbers from 0 – 255 to represent letters (lowercase & uppercase), symbols & special characters.

  22. ASCII CODE Get Picture from Internet

  23. Asc Function • Built-in function that returns the ASCII code corresponding to a character. • Form: Asc(string) • String – represents a string variable or a string in double quotes. Dim strChar As String strChar = “A” lblAns.Caption = Asc(strChar) ‘Displays 65

  24. Chr Function • Built-in function that returns the character corresponding to an ASCII code (decimal number) • Form: Chr(integer) • Integer is an integer variable or number that ranges from 0-255 Dim intAscii As Integer intAscii = 66 lblAns.Caption = Chr(intAscii) ‘Displays B

  25. Comparing Strings • Using relational operators to compare strings only compares the internal binary representation of characters (ASCII) • Example: “B” < “a” is True • “apple” = “Apple” is False

  26. StrComp(string1, string2, vbTextCompare) • Returns 0 if string1 and string2 are textually equal. • Returns –1 if string1 is less than string2 • Returns 1 if string1 is greater than string2.

  27. Like operators • Pattern matching – allows wildcards characters, character lists, and character ranges to match strings. • The Like operator is also used to perform a textual comparison on strings and uses pattern matching. • Form: result = string Like pattern • result will be a Boolean or Integer variable

  28. Pattern Symbols • ? – used in place of any single character • * - many characters • # - single digit • [] – enclose a list of characters • - - range of characters in a character list • , separate characters in a character list

More Related