280 likes | 460 Views
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.
E N D
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 • 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
Example of StrConv Dim strTitle As String strTitle = “summer fun” lblBook.Caption = StrConv(strTitle,vbProperCase) This will display “Summer Fun” to the label
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
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.
Example of Left(string, length) Dim strSeason As String strSeason = “Summer” lblDisplaySeason.Caption = Left(strSeason, 3)‘Sum will be display
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)
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
Mid Statement Similar to the Mid function. Used to replace a specified number of characters in a string. Form: Mid(string1, start, length) = string2
Example of Mid Statement Dim strSeason As String strSeason =“autumn” Mid(strSeason,2,3) = “summer” lblDisplay.Caption = strSeason ‘utu is replaced by sum ‘asummn
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.
Example of Len(string) Dim strSeason As String strSeason = “Summer” lblDisplayLength.Caption = Len(strSeason) ‘6 will be displayed
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.
Example InStr(start, string1, string2) Dim strSeason As String, strSubString As String strSeason = “Summer” strSubString = “um” lblDisplay.Caption = InStr(strSeason, strSubString) ‘ 2 is displayed
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.
Space function • Built-in function that returns a string of spaces • Form: Space(integer) • Integer represents the number of spaces in the returned string.
& 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
Example of the & operator strFirst = “Bob” strLast = “Smith” strFullName = strFirst & strLast ‘strFullName has BobSmith in it
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
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.
ASCII CODE Get Picture from Internet
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
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
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
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.
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
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