Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic th Edition

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic th Edition"— Presentation transcript:

1 Programming with Microsoft Visual Basic 2010 5th Edition
Chapter Eight String Manipulation

2 Previewing the Hangman Game Application
Simplified version of classic Hangman game Student must guess letter for mystery word If letter appears in word, it is added to blank dashes If letter does not appear in word, segment is added to hangman image Hangman image has nine lines and one circle Game is over when student correctly guesses all letters in word or has 10 incorrect guesses

3 Figure 8-1 Hangman Game application’s interface
Figure 8-2 Result of guessing the word

4 Lesson A Objectives After studying Lesson A, you should be able to:
Determine the number of characters in a string Remove characters from a string Insert characters in a string Align the characters in a string Search a string Access characters in a string Compare strings using pattern-matching

5 Working with Strings Applications often need to manipulate string data
Two scenarios involving string manipulation Determine first character of an inventory part number Search an address to find street name

6 Determining the Number of Characters in a String
Length property Stores number of characters contained in string Syntax: string.Length Returns integer value

7 Figure 8-3 Syntax and examples of the Length property

8 Removing Characters from a String
Trim method Removes space characters from both ends of string Remove method Removes specified number of characters located anywhere in a string Computer makes temporary copy of string in memory, then performs specified removal on copy Original string is not changed Modified copy is returned to program

9 Figure 8-4 Syntax and examples of the Trim and Remove methods

10 The Product ID Application
Product ID application displays listing of product IDs entered by user Each product ID must contain exactly five characters

11 Figure 8-5 btnAdd control’s Click event procedure

12 Inserting Characters in a String
Insert method Inserts 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 argument Integer specifying starting location to insert value Represents the position in a string To insert at beginning of string: Use startIndex of 0

13 Figure 8-6 Syntax and examples of the Insert method

14 Aligning the Characters in a String
PadLeft method Inserts zero or more characters at the beginning of string Results in string of a specified length Right-aligns the characters within the string PadRight method Inserts characters at the end of the string Left-aligns the characters within the string

15 Figure 8-7 Syntax and examples of the PadLeft and PadRight methods

16 The Net Pay Application
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

17 Figure 8-8 btnFormat control’s Click event procedure

18 Searching a String Contains method
Used to search string to find specific sequence of characters Returns a Boolean value True if found False otherwise Must specify characters you are searching for Begins search with first character in string

19 Searching a String (cont’d.)
IndexOf method Determine 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 search If not specified, starts searching with first character in string

20 Figure 8-10 Syntax and examples of the Contains and IndexOf methods (continues)

21 Figure 8-10 Syntax and examples of the Contains and IndexOf methods (cont’d.)

22 The City and State Application
Allows user to enter string: Composed of city name, comma, space, and state name Displays index of the comma in the string

23 Figure 8-11 btnLocate control’s Click event procedure

24 The City and State Application (cont’d.)
Figure 8-12 Interface showing the comma’s index

25 Accessing the Characters in a String
Substring method Used to access any number of characters in string Returns string with specified number of characters Specify index of first character to access in string and number of characters to retrieve If number of characters is not specified, method returns all characters from startIndex position to the end of string

26 Figure 8-13 Syntax and examples of the Substring method

27 The Rearrange Name Application
Allows user to enter first name, space, and last name Displays name as last name, comma, space, first name

28 Figure 8-14 btnRearrange control’s Click event procedure

29 The Rearrange Name Application (cont’d.)
Figure 8-15 Interface showing the rearranged name

30 Using Pattern-Matching to Compare Strings
Like operator Allows use of pattern-matching characters to determine whether one string is equal to another Must specify string to be examined and pattern to be matched Pattern can contain pattern-matching characters Returns Boolean value Returns True if match is made, False otherwise

31 Figure 8-16 Syntax and examples of the Like operator (continues)

32 Figure 8-16 Syntax and examples of the Like operator (continues)

33 Figure 8-16 Syntax and examples of the Like operator (continues)

34 Using Pattern-Matching to Compare Strings (cont’d.)
Figure 8-16 Syntax and examples of the Like operator (cont’d.)

35 Modifying the Product ID Application
User enters product ID values of exactly five characters Modification will ensure that entered five characters consist of three letters followed by two numbers

36 Figure 8-17 Modified Click event procedure for the btnAdd control

37 Lesson A Summary Visual Basic includes various string manipulation techniques Properties: Length property Methods: Trim, Remove, Insert, Contains, IndexOf, Substring, PadLeft, PadRight Operators: Like operator Figure 8-18 summarizes syntax and purpose of each

38 Lesson B Objectives After studying Lesson B, you should be able to:
Include a MenuStrip control on a form Add elements to a menu Assign access keys to menu elements Assign shortcut keys to commonly used menu items Code a menu item’s Click event procedure

39 Adding a Menu to a Form MenuStrip control Menu title
Used to include one or more menus on a form Menu title Appears on menu bar at top of form Menu items can include: Commands, submenu items, or separator bars Clicking command on menu executes it Clicking submenu item opens additional menu Separator bars provides visual grouping

40 Adding a Menu to a Form (cont’d.)
Figure 8-19 Location of menu elements

41 Adding a Menu to a Form (cont’d.)
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 standards Use book title capitalization for menu item captions Ellipsis (…) after item caption indicates menu item requires more information File menu should be first item on menu bar Cut, Copy, Paste should appear on Edit menu

42 Figure 8-20 MenuStrip control added to the form

43 Adding a Menu to a Form (cont’d.)
Figure 8-22 Drop-down list

44 Adding a Menu to a Form (cont’d.)
Figure 8-23 File menu opened during run time

45 Assigning Shortcut Keys to Menu Items
Appear to right of menu item Allow you to select item without opening menu Example: Ctrl+X and Ctrl+V Cut and paste commands Assign shortcut keys to commonly used menu items Follow Windows standard conventions Shortcut keys can be used when menu is closed

46 Assigning Shortcut Keys to Menu Items (cont’d.)
Figure 8-24 Shortcut key specified in the ShortcutKeys box

47 Assigning Shortcut Keys to Menu Items (cont’d.)
Figure 8-25 Location of the shortcut key on the menu

48 Coding the Exit Menu Item
How to end Hangman Game application User clicks Exit item on File menu Exit item’s Click event procedure calls Me.Close()

49 Lesson B Summary MenuStrip control
Creates 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

50 Lesson C Objectives After studying Lesson C, you should be able to:
Include the Length property in a procedure Include the Substring method in a procedure Include the Like operator in a procedure Include the Remove method in a procedure Include the Insert method in a procedure Include the Contains method in a procedure

51 Completing the Hangman Game Application
Application requirements Allow one student to enter five-letter word Allow another student to guess word, letter by letter Two events can end game Second student guesses all letters in word Second student makes 10 incorrect guesses

52 Coding the mnuFileNew Object’s Click Event Procedure
mnuFileNew object’s Click event procedure is invoked when user clicks New Game option on File menu Two ways to begin new Hangman game Click File on menu bar, then click New Game Press Ctrl + N

53 Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
Figure 8-29 Additional comments and code entered in the procedure

54 Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
Figure 8-30 Comment and selection structure’s true path

55 Figure 8-31 Additional comments and selection structure entered in the procedure

56 Figure 8-32 Additional comments and selection structures entered in the procedure

57 Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
Figure 8-33 Final comment and selection structure entered in the procedure

58 Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
Figure 8-35 Result of entering the first incorrect letter

59 Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
Figure 8-36 Result of guessing the word

60 Coding the mnuFileNew Object’s Click Event Procedure (cont’d.)
Figure 8-37 Result of making 10 incorrect guesses

61 Lesson C Summary 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


Download ppt "Programming with Microsoft Visual Basic th Edition"

Similar presentations


Ads by Google