Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)

Similar presentations


Presentation on theme: "Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)"— Presentation transcript:

1 Strings PART I STRINGS, DATES, AND TIMES

2 FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI) Character Set Small integer values 0 to 127(1st 128 ANSI Characters) equal American Standard Code for Information Interchange(ASCII) Strings are series of characters treated as single unit include: letters, numbers, special characters, and others (special VB data type)

3 FUNDAMENTALS OF CHARATERS AND STRINGS String literals or string constants indicated by double quotes Example: “John Q. Doe” “9999 Main Street” “Somewhere, Massachusetts” “(555) 555-5555” Dollar sign is type declaration character for String String data type: declares string variables

4 FUNDAMENTALS OF CHARATERS AND STRINGS Types of strings: 1. variable-length (default) 2. fixed-length Consists of characters with numeric values, range 0 to 255 grow and shrink dynamically Example: Variable-length Dim s As String s = “blue”

5 FUNDAMENTALS OF CHARATERS AND STRINGS Example: Fixed length Dim SocialSecurityNumber As String * 11 SocialSecurityNumber = “212-45-6363” String Concatenation with & and + : combine smaller strings into one Example: s1 = “Pro” s2 = “gram” s3 = s1 & s2 output=program or s3 = s1 + s2

6 FUNDAMENTALS OF CHARATERS AND STRINGS Problem: s1 = “hello” + 22 would attempt convert hello to number and add 22 resulting in type mismatch Rule of thumb: use & for concatenation Comparing Character Strings: use: relational operators (, =) equality operators (<>, =)

7 FUNDAMENTALS OF CHARATERS AND STRINGS StrComp function: returns: 0 if strings are equal -1 if 1st string is < 2nd string 1 if 1st string is > 2nd string Comparisons are made based on numeric values (ASCII) associated with characters Function StrComp has an optional 3rd argument: indicates comparison type

8 FUNDAMENTALS OF CHARATERS AND STRINGS Option Compare type (module-level statement) type: Binary, Text, or Database Option Compare not used: default is Binary Strings of different length: Example: “j” VS. “john” if one name is equivalent to leftmost portion of another name, shorter name comes before longer name

9 FUNDAMENTALS OF CHARATERS AND STRINGS Operator Like another comparison of two strings compare patterns of characters as well as strings Example: “HBLT55DD” Like “HBLT55DD” True or False test “HBLT55DD” Like “HBLT*” asterisk is pattern matching character any number of characters can follow

10 FUNDAMENTALS OF CHARATERS AND STRINGS question mark- single character can be any type of character pound sign- single character can be a digit Example: “HBLT55DD” Like “?#LT55DD” (false) “HBLT55DD” Like “?BLT##DD” (true) square brackets- series of characters provided for matching characters Example: “HBLT55DD” Like “H[A-F]LT55DD” “HBLT55DD” Like “H[A-F]LT[!4-7]5DD” [!] not

11 MANUIPULATING INDIVIDUAL CHARACTERS Code example: Figure 8.3, p. 310 Mid$ - allows programmer to extract one character at a time from a string Example: Figure 8.4, p. 311 Mid$ used with phrase to extract one character at a time in reverse Note1: program also uses Len function to determine length of phrase

12 MANUIPULATING INDIVIDUAL CHARACTERS Mid$ Function Arguments: 1. Source string from which substring will be selected 2. Starting character position in string 3. Number of characters to select Note2: if last argument is omitted or number of characters remaining is less than number of characters, remainder of string from starting character position is returned

13 MANUIPULATING INDIVIDUAL CHARACTERS Example: phrase = txtInput.Text txtOutput.Text = txtOutput.Text & _ Mid$ (phrase, position, 1) Note3: can be used to replace portion of string with another string Example: x =“Visual Basic 6!” Mid$ (x,2,3) = “xxx” x changed to “Vxxxal Basic 6!”

14 LEFT$,RIGHT$, AND INSTR Left$ selects left most portion of string Example: s1=“ABCDEF” s2=“Left$(s1,4) Assigns leftmost four character to s2 s2 = “ABCD” Right$ selects rightmost portion of string s1 = “ABCDEF” s2 = Right$(s1,4) Assigns rigthmost four characters to s2

15 LEFT$,RIGHT$, AND INSTR s2 = “CDEF” InStr search through one string (base string) to determine if it contains another string (search string) When found, starting character location of string is returned Should second string contain no character (0 length), starting position is returned Example: s1 = “AEIOU” s2 = “IOU”

16 LEFT$,RIGHT$, AND INSTR result = InStr(1, s1, s2) result = “IOU” (starts search at position 1 of s1) Note1: If InStr determines search string is not contained within base string, returns zero Example: result = InStr(1, “aeiou”, “aeb”) result = InStr(4, “aeiou”, “iou”) result = InStr(1, “aeiou”, “aeiouy”) result = 0 for all three cases

17 LEFT$,RIGHT$, AND INSTR (1) blankPosition = InStr(1, phrase, “ “) returns first blank position in phrase (2) nextWord = Left$(phrase, blankPosition - 1) picks off one word at a time from phrase and assigned to nextWord (3) phrase = Right$(phrase, Len(phrase) - blankPosition)

18 LEFT$,RIGHT$, AND INSTR returns remainder of phrase from character after blankPosition Note2: eventually phrase shrinks to one word Searching for Substrings in Strings Using InStr and InstrRev: search for substrings in a string from beginning to end of string InStr searches from any location in a string InStrRev search from end or any other position

19 Searching for Substrings in Strings Using InStr and InstrRev InStrRev’s arguments: (1) base string (2) search string (3) starting character position in base string Outcome” (1) if search string found, starting character location of string is returned (2) if search string is zero length, starting position is returned

20 Searching for Substrings in Strings Using InStr and InstrRev Example1: Dim s1 As string, s2 As string s1 = “abcdefghijklmnop” s2 = “m” result = InStrRev (s1, s2, Len (s1)) determines s2 is in s1 at position 13 return 13 as starting position (s2 in s1) 13 is assigned to result Len (s1)- s2 begins at end of s1

21 Searching for Substrings in Strings Using InStr and InstrRev Note: third argument can be omitted if search begins from end of base string if string not contained in base string, returns 0 Example2: result = InStr(“aeiou”, “aeb”) result = InStr(“aeiou”, “iou”, 2) resutl = InStr(“aeiou”, “aeiouy”) fowardResult = InStr(Input1, Input2) locates 1st occurrence of Input2 in Input1

22 Searching for Substrings in Strings Using InStr and InstrRev backwardResult = InStrRev(txtInput1.Text, txtInput2.Text) locates last occurrence of txtInput2.Text in txtInput1.Text starting from end of txtInput1 LTrim$, Rtrim$, Trim$: (1) remove leading spaces from left side of string (2) remove trailing spaces from right side of string (3) remove spaces on left and right side of string

23 Ltrim$, Rtrim$, Trim$ Useful for removing extra space characters used to pad a fixed-length string string would occupy space allocated String$ and Space$: create strings of specified number of characters String$ creates string of specified character Space$ creates string of spaces


Download ppt "Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)"

Similar presentations


Ads by Google