Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation."— Presentation transcript:

1 Programming with Microsoft Visual Basic 2008 Fourth 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 2Programming with Microsoft Visual Basic 2008, Fourth Edition

3 Previewing the Hangman Game Application (continued) 3Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-1: Hangman Game application’s interface

4 Lesson A Objectives After studying Lesson A, you should be able to: Determine the number of characters in a string Remove spaces from the beginning and end of a string Replace characters in a string Insert characters in a string Search a string Access characters in a string Compare strings using pattern-matching Programming with Microsoft Visual Basic 2008, Fourth Edition4

5 Working with Strings Applications often need to manipulate string data Two scenarios involving string manipulation –Determine first letter of an inventory part id –Search an address to find street name 5Programming with Microsoft Visual Basic 2008, Fourth Edition

6 Determining the Number of Characters in a String Length property: –Stores number of characters contained in string Syntax: string.Length Returns integer value 6Programming with Microsoft Visual Basic 2008, Fourth Edition

7 Determining the Number of Characters in a String (continued) 7Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-3: Syntax, purpose, and examples of the Length property

8 Removing Spaces from the Beginning and End of a String Trim method: –Removes spaces from both ends of string Computer makes temporary copy of string in memory, then performs trimming on copy –Original string is not changed –Modified copy is returned to program 8Programming with Microsoft Visual Basic 2008, Fourth Edition

9 Removing Spaces from the Beginning and End of a String (continued) 9Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-4: Syntax, purpose, and example of the Trim method

10 The Product ID Application Product ID application displays listing of product IDs entered by user Each product ID must contain exactly 5 characters 10Programming with Microsoft Visual Basic 2008, Fourth Edition

11 The Product ID Application (continued) 11Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-5: Completed Click event procedure for the btnAdd control

12 Replacing Characters in a String Replace method: –Replaces one sequence of characters with another Example: Replace area code “800” with “877” Must specify both sequence of characters in string to be replaced, and replacement characters to be used Computer makes temporary copy of string and replaces specified characters in copy Returns string that includes replaced characters 12Programming with Microsoft Visual Basic 2008, Fourth Edition

13 Replacing Characters in a String (continued) 13Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-6: Syntax, purpose, and examples of the Replace method

14 The Mid Statement Mid statement: –Replaces set of characters with another string Must specify: –targetString: String targeted for character replacement –replacementString: Contains replacement characters to be used –start: Position of first character of targetString where replacement should take place –count: Number of characters to replace in targetString 14Programming with Microsoft Visual Basic 2008, Fourth Edition

15 The Mid Statement (continued) 15Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-7: Syntax, purpose, and examples of the Mid statement

16 The Phone Number Application Phone number application: –Allows user to enter phone number in 999-999- 9999 format –Displays phone number without hyphens and with area code changed to 800 16Programming with Microsoft Visual Basic 2008, Fourth Edition

17 The Phone Number Application (continued) 17Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-9: Modified phone numbers displayed in the interface

18 Inserting Characters in a String PadLeft method: –Inserts padded characters at start of string –Right-aligns characters within string PadRight method: –Inserts padded characters at end of string –Left-aligns characters within string Must specify character to pad with, and desired final total length of target string –If no character is specified, space is used 18Programming with Microsoft Visual Basic 2008, Fourth Edition

19 Insert Method Insert method: –Inserts characters anywhere within string Examples: –Insert middle initial within employee name –Insert parentheses around area code in phone number Must specify position (index) where new characters will be inserted 19Programming with Microsoft Visual Basic 2008, Fourth Edition

20 Insert Method (continued) 20Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-11: Syntax, purpose, and example of the Insert method

21 The Net Pay Application 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 21Programming with Microsoft Visual Basic 2008, Fourth Edition

22 The Net Pay Application (continued) 22Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-12: btnFormat control’s Click event procedure

23 The Net Pay Application (continued) 23Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-13: Formatted net pay displayed in the interface

24 Searching a String Contains method: –Used to search string to find specific sequence of characters Examples: –Determine if area code appears in phone number –Determine if street name appears in address Must specify characters you are searching for Returns a Boolean value –True if found –False otherwise 24Programming with Microsoft Visual Basic 2008, Fourth Edition

25 The IndexOf Method 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 in string, and index of character at which to begin search 25Programming with Microsoft Visual Basic 2008, Fourth Edition

26 The IndexOf Method (continued) The City and State Application: –Allows user to enter string containing city, comma, space, and state –Displays index of comma in string Programming with Microsoft Visual Basic 2008, Fourth Edition26

27 The IndexOf Method (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition27 Figure 8-16: btnLocate control’s Click event procedure

28 The IndexOf Method (continued) 28Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-17: Comma’s index shown in the interface

29 Accessing Characters Contained in a String Substring method: –Used to access any number of characters in string –Returns string with specified number of characters Must specify index of first character to access in string, and number of characters to retrieve –If number of characters is not specified, all characters from start position to the end of string are returned 29Programming with Microsoft Visual Basic 2008, Fourth Edition

30 Accessing Characters Contained in a String (continued) 30Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-18: Syntax, purpose, and examples of the Substring methodc

31 The Rearrange Name Application Rearrange Name application: –Allows user to enter first name, space, and last name –Displays name as last name, comma, space, first name Programming with Microsoft Visual Basic 2008, Fourth Edition31

32 Programming with Microsoft Visual Basic 2008, Fourth Edition32 Figure 8-19: btnRearrange control’s Click event procedure The Rearrange Name Application (continued)

33 Programming with Microsoft Visual Basic 2008, Fourth Edition33 Figure 8-20: Rearranged name shown in the interface

34 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 34Programming with Microsoft Visual Basic 2008, Fourth Edition

35 Using Pattern-Matching to Compare Strings (continued) 35Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-21: Syntax, purpose, and examples of the Like operator

36 Modifying the Product ID Application 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 Programming with Microsoft Visual Basic 2008, Fourth Edition36

37 Modifying the Product ID Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition37 Figure 8-22: Modified btnAdd Click event procedure

38 Lesson A Summary Length property: Determines string length Trim method: Removes spaces from beginning and end of string Replace method: Replaces characters with new values Mid statement: Replaces specific number of characters with other characters PadLeft and PadRight methods: Pads beginning and ending of string Insert method: Inserts characters in string 38Programming with Microsoft Visual Basic 2008, Fourth Edition

39 Lesson A Summary (continued) Contains method: Searches string to determine if it contains specific sequence of characters IndexOf method: Searches string and returns index where specified characters appear Substring method: Accesses one or more characters contained in string Like operator: Compares two strings using pattern-matching characters 39Programming with Microsoft Visual Basic 2008, Fourth Edition

40 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 40Programming with Microsoft Visual Basic 2008, Fourth Edition

41 Adding a Menu to a Form MenuStrip control: Used to include one or more menus in application 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 41Programming with Microsoft Visual Basic 2008, Fourth Edition

42 Adding a Menu to a Form (continued) 42Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-24: Location of menu elements

43 Adding a Menu to a Form (continued) 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: –Ellipsis (…) after item caption indicates dialog box will display for user input –File menu should be first item on menu bar –Cut, Copy, Paste should appear on Edit menu 43Programming with Microsoft Visual Basic 2008, Fourth Edition

44 44 Figure 8-25: MenuStrip control added to the form Adding a Menu to a Form (continued)

45 Programming with Microsoft Visual Basic 2008, Fourth Edition45 Figure 8-26: Menu title included on the form

46 Adding a Menu to a Form (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition46 Figure 8-27: Drop-down list

47 Assigning Shortcut Keys to Menu Items Shortcut keys: –Appear to right of menu item –Allow you to select item without opening menu Example: Ctrl+S executes Save in MS Word Assign shortcut keys to commonly used menu items –Follow Windows standard conventions Shortcut keys can be used when menu is closed 47Programming with Microsoft Visual Basic 2008, Fourth Edition

48 Assigning Shortcut Keys to Menu Items (continued) 48Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-29: Shortcut key specified in the ShortcutKeys box

49 Assigning Shortcut Keys to Menu Items (continued) 49Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-30: Location of the shortcut key on the menu

50 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() 50Programming with Microsoft Visual Basic 2008, Fourth Edition

51 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 Assign shortcut keys to commonly used menu items Follow Windows standard when creating menus 51Programming with Microsoft Visual Basic 2008, Fourth Edition

52 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 Mid statement in a procedure Include the Contains method in a procedure 52Programming with Microsoft Visual Basic 2008, Fourth Edition

53 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 53Programming with Microsoft Visual Basic 2008, Fourth Edition

54 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 54Programming with Microsoft Visual Basic 2008, Fourth Edition

55 Coding the mnuFileNew Object’s Click Event Procedure (continued) 55Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-34: Additional comments and code entered in the procedure

56 Coding the mnuFileNew Object’s Click Event Procedure (continued) 56Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-35: Comment and selection structure’s true path

57 Coding the mnuFileNew Object’s Click Event Procedure (continued) 57Programming with Microsoft Visual Basic 2008, Fourth Edition Figure 8-36: Comments and selection structure entered in the procedure

58 Coding the mnuFileNew Object’s Click Event Procedure (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition58 Figure 8-37: Additional comments and selection structures entered in the procedure

59 Coding the mnuFileNew Object’s Click Event Procedure (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition59 Figure 8-38: Final comment and selection structure entered in the procedure

60 Coding the mnuFileNew Object’s Click Event Procedure (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition60 Figure 8-41: Result of guessing the word

61 Coding the mnuFileNew Object’s Click Event Procedure (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition61 Figure 8-42: Result of making 10 incorrect guesses

62 Lesson C Summary Hangman application uses various methods to manipulate strings Length property: Provides length of string Substring method: Accesses characters in string Like operator: Uses pattern matching to compare two strings Mid statement: Replaces one character with another Contains method: Determines whether specific character is within string 62Programming with Microsoft Visual Basic 2008, Fourth Edition


Download ppt "Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation."

Similar presentations


Ads by Google