chapter eight string manipulation programming with microsoft visual basic 2010 5 th edition
TRANSCRIPT
Chapter EightString Manipulation
Programming with Microsoft Visual Basic 2010
5th Edition
Programming with Microsoft Visual Basic 2010, 5th Edition
Previewing the Hangman Game Application
2
Simplified version of classic Hangman game
Student must guess letter for mystery wordIf letter appears in word, it is added to blank
dashesIf letter does not appear in word, segment is
added to hangman imageHangman image has nine lines and one
circleGame is over when student correctly guesses
all letters in word or has 10 incorrect guesses
Programming with Microsoft Visual Basic 2010, 5th Edition3
Figure 8-1 Hangman Game application’s interface
Figure 8-2 Result of guessing the word
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson A Objectives
4
After studying Lesson A, you should be able to:
Determine the number of characters in a string
Remove characters from a stringInsert characters in a stringAlign the characters in a stringSearch a stringAccess characters in a stringCompare strings using pattern-matching
Programming with Microsoft Visual Basic 2010, 5th Edition
Working with Strings
5
Applications often need to manipulate string data
Two scenarios involving string manipulation Determine first character of an inventory part
numberSearch an address to find street name
Programming with Microsoft Visual Basic 2010, 5th Edition
Determining the Number of Characters in a String
6
Length propertyStores number of characters contained in
stringSyntax: string.LengthReturns integer value
Programming with Microsoft Visual Basic 2010, 5th Edition7
Figure 8-3 Syntax and examples of the Length property
Programming with Microsoft Visual Basic 2010, 5th Edition
Removing Characters from a String
8
Trim methodRemoves space characters from both ends of
stringRemove method
Removes specified number of characters located anywhere in a string
Computer makes temporary copy of string in memory, then performs specified removal on copyOriginal string is not changedModified copy is returned to program
Programming with Microsoft Visual Basic 2010, 5th Edition9
Figure 8-4 Syntax and examples of the Trim and Remove methods
Programming with Microsoft Visual Basic 2010, 5th Edition
The Product ID Application
10
Product ID application displays listing of product IDs entered by user
Each product ID must contain exactly five characters
Programming with Microsoft Visual Basic 2010, 5th Edition11
Figure 8-5 btnAdd control’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
Inserting Characters in a String
12
Insert methodInserts characters anywhere in a string
Computer makes temporary copy of string and inserts specified characters into the copy
Returns string with appropriate characters inserted
startIndex argumentInteger specifying starting location to insert value
Represents the position in a stringTo insert at beginning of string:
Use startIndex of 0
Programming with Microsoft Visual Basic 2010, 5th Edition13
Figure 8-6 Syntax and examples of the Insert method
Programming with Microsoft Visual Basic 2010, 5th Edition
Aligning the Characters in a String
14
PadLeft methodInserts zero or more characters at the
beginning of stringResults in string of a specified lengthRight-aligns the characters within the string
PadRight methodInserts characters at the end of the stringResults in string of a specified lengthLeft-aligns the characters within the string
Programming with Microsoft Visual Basic 2010, 5th Edition15
Figure 8-7 Syntax and examples of the PadLeft and PadRight methods
Programming with Microsoft Visual Basic 2010, 5th Edition
The Net Pay Application
16
Allows user to enter amount of employee’s net pay
Displays net pay with leading dollar sign, asterisks, and two decimal places
Uses the Insert and PadLeft methods
Programming with Microsoft Visual Basic 2010, 5th Edition17
Figure 8-8 btnFormat control’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
Searching a String
18
Contains methodUsed to search string to find specific
sequence of charactersReturns a Boolean value
True if foundFalse otherwise
Must specify characters you are searching for
Begins search with first character in string
Programming with Microsoft Visual Basic 2010, 5th Edition
Searching a String (cont’d.)
19
IndexOf methodDetermine if string contains character
sequence Returns integer specifying start position of
found character sequence (substring)If substring is not found, method returns -1
Must specify sequence of characters to search for
May specify index of character at which to begin searchIf not specified, starts searching with first
character in string
Programming with Microsoft Visual Basic 2010, 5th Edition20
Figure 8-10 Syntax and examples of the Contains and IndexOf methods (continues)
Programming with Microsoft Visual Basic 2010, 5th Edition21
Figure 8-10 Syntax and examples of the Contains and IndexOf methods (cont’d.)
Programming with Microsoft Visual Basic 2010, 5th Edition
The City and State Application
22
Allows user to enter string:Composed of city name, comma, space, and
state nameDisplays index of the comma in the string
Programming with Microsoft Visual Basic 2010, 5th Edition23
Figure 8-11 btnLocate control’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
The City and State Application (cont’d.)
24
Figure 8-12 Interface showing the comma’s index
Programming with Microsoft Visual Basic 2010, 5th Edition
Accessing the Characters in a String
25
Substring methodUsed to access any number of characters in
stringReturns string with specified number of
charactersSpecify index of first character to access in
string and number of characters to retrieveIf number of characters is not specified,
method returns all characters from startIndex position to the end of string
Programming with Microsoft Visual Basic 2010, 5th Edition26
Figure 8-13 Syntax and examples of the Substring method
Programming with Microsoft Visual Basic 2010, 5th Edition
The Rearrange Name Application
27
Rearrange Name applicationAllows user to enter first name, space, and
last nameDisplays name as last name, comma, space,
first name
Programming with Microsoft Visual Basic 2010, 5th Edition28
Figure 8-14 btnRearrange control’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
The Rearrange Name Application (cont’d.)
29
Figure 8-15 Interface showing the rearranged name
Programming with Microsoft Visual Basic 2010, 5th Edition
Using Pattern-Matching to Compare Strings
30
Like operatorAllows use of pattern-matching characters to
determine whether one string is equal to another
Must specify string to be examined and pattern to be matchedPattern can contain pattern-matching
charactersReturns Boolean value
Returns True if match is made, False otherwise
Programming with Microsoft Visual Basic 2010, 5th Edition31
Figure 8-16 Syntax and examples of the Like operator (continues)
Programming with Microsoft Visual Basic 2010, 5th Edition32Figure 8-16 Syntax and examples of the Like operator (continues)
Programming with Microsoft Visual Basic 2010, 5th Edition33
Figure 8-16 Syntax and examples of the Like operator (continues)
Programming with Microsoft Visual Basic 2010, 5th Edition
Using Pattern-Matching to Compare Strings (cont’d.)
34
Figure 8-16 Syntax and examples of the Like operator (cont’d.)
Programming with Microsoft Visual Basic 2010, 5th Edition
Modifying the Product ID Application
35
Product ID applicationUser enters product ID values of exactly five
charactersModification will ensure that entered five
characters consist of three letters followed by two numbers
Programming with Microsoft Visual Basic 2010, 5th Edition36
Figure 8-17 Modified Click event procedure for the btnAdd control
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson A Summary
37
Visual Basic includes various string manipulation techniquesProperties: Length propertyMethods: Trim, Remove, Insert, Contains,
IndexOf, Substring, PadLeft, PadRightOperators: Like operator
Figure 8-18 summarizes syntax and purpose of each
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson B Objectives
38
After studying Lesson B, you should be able to:
Include a MenuStrip control on a formAdd elements to a menuAssign access keys to menu elementsAssign shortcut keys to commonly used
menu itemsCode a menu item’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
Adding a Menu to a Form
39
MenuStrip controlUsed to include one or more menus on a
formMenu title
Appears on menu bar at top of formMenu items can include:
Commands, submenu items, or separator bars
Clicking command on menu executes itClicking submenu item opens additional
menu Separator bars provides visual grouping
Programming with Microsoft Visual Basic 2010, 5th Edition
Adding a Menu to a Form (cont’d.)
40
Figure 8-19 Location of menu elements
Programming with Microsoft Visual Basic 2010, 5th Edition
Adding a Menu to a Form (cont’d.)
41
Menu title captions should be one word only
Menu item captions can be from one to three words
Assign unique access keys to menu titles and items
Follow Windows menu standardsUse book title capitalization for menu item
captionsEllipsis (…) after item caption indicates menu
item requires more informationFile menu should be first item on menu barCut, Copy, Paste should appear on Edit menu
Programming with Microsoft Visual Basic 2010, 5th Edition42
Figure 8-20 MenuStrip control added to the form
Programming with Microsoft Visual Basic 2010, 5th Edition
Adding a Menu to a Form (cont’d.)
43
Figure 8-22 Drop-down list
Programming with Microsoft Visual Basic 2010, 5th Edition
Adding a Menu to a Form (cont’d.)
44
Figure 8-23 File menu opened during run time
Programming with Microsoft Visual Basic 2010, 5th Edition
Assigning Shortcut Keys to Menu Items
45
Shortcut keysAppear to right of menu itemAllow you to select item without opening
menuExample: Ctrl+X and Ctrl+V
Cut and paste commandsAssign shortcut keys to commonly used
menu itemsFollow Windows standard conventions
Shortcut keys can be used when menu is closed
Programming with Microsoft Visual Basic 2010, 5th Edition
Assigning Shortcut Keys to Menu Items (cont’d.)
46
Figure 8-24 Shortcut key specified in the ShortcutKeys box
Programming with Microsoft Visual Basic 2010, 5th Edition
Assigning Shortcut Keys to Menu Items (cont’d.)
47
Figure 8-25 Location of the shortcut key on the menu
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the Exit Menu Item
48
How to end Hangman Game applicationUser clicks Exit item on File menuExit item’s Click event procedure calls Me.Close()
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson B Summary
49
MenuStrip controlCreates menu on form
Menu items can include commands, submenu items, and separator bars
Assign unique access key to each menu element:Except separator bars
Assign shortcut keys to commonly used menu items
Follow Windows standard when creating menus
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson C Objectives
50
After studying Lesson C, you should be able to:
Include the Length property in a procedureInclude the Substring method in a
procedureInclude the Like operator in a procedureInclude the Remove method in a procedureInclude the Insert method in a procedureInclude the Contains method in a procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
Completing the Hangman Game Application
51
Application requirementsAllow one student to enter five-letter wordAllow another student to guess word, letter
by letter Two events can end game
Second student guesses all letters in wordSecond student makes 10 incorrect guesses
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the mnuFileNew Object’s Click Event Procedure
52
mnuFileNew object’s Click event procedure is invoked when user clicks New Game option on File menu
Two ways to begin new Hangman gameClick File on menu bar, then click New GamePress Ctrl + N
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
53
Figure 8-29 Additional comments and code entered in the procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
54
Figure 8-30 Comment and selection structure’s true path
Programming with Microsoft Visual Basic 2010, 5th Edition55
Figure 8-31 Additional comments and selection structure entered in the procedure
Programming with Microsoft Visual Basic 2010, 5th Edition56
Figure 8-32 Additional comments and selection structures entered in the procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
57
Figure 8-33 Final comment and selection structure entered in the procedure
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
58
Figure 8-35 Result of entering the first incorrect letter
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
59
Figure 8-36 Result of guessing the word
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
60
Figure 8-37 Result of making 10 incorrect guesses
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson C Summary
61
Use string’s Length property to determine length of string
Use Substring method to access one or more characters in a string
Use the Like operator to compare two strings
Use the Remove method to remove specified characters anywhere in a string
Use Insert method to insert characters in a string
Use Contains method to determine whether a specific character is contained in a string