Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8: String Manipulation

Similar presentations


Presentation on theme: "Chapter 8: String Manipulation"— Presentation transcript:

1 Chapter 8: String Manipulation

2 Previewing the Frankenstein Game Application
Figure 8-1 Interface for the Frankenstein Game application Programming with Microsoft Visual Basic 2012

3 Previewing the Frankenstein Game Application (cont.)
Figure 8-2 Interface after guessing the secret word Programming with Microsoft Visual Basic 2012

4 Previewing the Frankenstein Game Application (cont.)
Figure 8-3 Interface after not guessing the secret word Programming with Microsoft Visual Basic 2012

5 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 Programming with Microsoft Visual Basic 2012

6 Determining the Number of Characters in a String
Applications often need to manipulate string data There are two scenarios involving string manipulation: Determine the first character of an inventory part number Search an address to find a street name Length property Stores the number of characters contained in a string Syntax: string.Length Returns an integer value Programming with Microsoft Visual Basic 2012

7 Determining the Number of Characters in a String (cont.)
Figure 8-4 Syntax and examples of the Length property Programming with Microsoft Visual Basic 2012

8 Removing Characters from a String
Trim method Removes space characters from both ends of a string Remove method Removes a specified number of characters located anywhere in a string The computer makes a temporary copy of the string in memory, and then performs the specified removal on the copy The original string is not changed The modified copy is returned to the program Programming with Microsoft Visual Basic 2012

9 Removing Characters from a String (cont.)
Figure 8-5 Syntax and examples of the Trim and Remove methods Programming with Microsoft Visual Basic 2012

10 Removing Characters from a String (cont.)
The Product ID Application Figure 8-7 Sample run of the Product ID application Figure 8-6 btnAdd control’s Click event procedure Programming with Microsoft Visual Basic 2012

11 Inserting Characters in a String
Insert method Inserts characters anywhere in a string The computer makes a temporary copy of a string and inserts specified characters into the copy Returns a string with the appropriate characters inserted startIndex argument An integer specifying the starting location to insert the value Represents the position in a string To insert the value at the beginning of a string, use a startIndex of 0 Programming with Microsoft Visual Basic 2012

12 Inserting Characters in a String (cont.)
Figure 8-8 Syntax and examples of the Insert method Programming with Microsoft Visual Basic 2012

13 Inserting Characters in a String (cont.)
Aligning the Characters in a String PadLeft method Inserts zero or more characters at the beginning of a string Results in a 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 Programming with Microsoft Visual Basic 2012

14 Inserting Characters in a String (cont.)
Aligning the Characters in a String (cont.) Figure 8-9 Syntax and examples of the PadLeft and PadRight methods Programming with Microsoft Visual Basic 2012

15 Inserting Characters in a String (cont.)
The Net Pay Application Allows the user to enter the amount of the employee’s net pay Displays the net pay with a leading dollar sign, asterisks, and two decimal places Uses the Insert and PadLeft methods Programming with Microsoft Visual Basic 2012

16 Inserting Characters in a String (cont.)
The Net Pay Application (cont.) Figure 8-11 Interface showing the formatted net pay Figure 8-10 btnFormat_Click procedure Programming with Microsoft Visual Basic 2012

17 Searching a String Contains method
Used to search a string to find a specific sequence of characters Returns a Boolean value True if found False otherwise You must specify the characters you are searching for The search begins with the first character in the string Programming with Microsoft Visual Basic 2012

18 Searching a String (cont.)
IndexOf method Determines if a string contains a character sequence Returns an integer specifying the start position of a found character sequence (substring) If the substring is not found, the method returns -1 You must specify the sequence of characters to search for You may specify the index of the character at which to begin the search If not specified, the search begins with the first character in the string Programming with Microsoft Visual Basic 2012

19 Searching a String (cont.)
Figure 8-12 Syntax and examples of the Contains and IndexOf methods (continues) Programming with Microsoft Visual Basic 2012

20 Searching a String (cont.)
Figure 8-12 Syntax and examples of the Contains and IndexOf methods Programming with Microsoft Visual Basic 2012

21 Searching a String (cont.)
The City and State Application Allows the user to enter a string Composed of a city name, a comma, a space, and a state name Displays index of the comma in the string Figure 8-14 Interface showing the comma’s index Figure 8-13 btnLocate_Click procedure Programming with Microsoft Visual Basic 2012

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

23 Accessing the Characters in a String (cont.)
Figure 8-15 Syntax and examples of the Substring method Programming with Microsoft Visual Basic 2012

24 Accessing the Characters in a String (cont.)
The Rearrange Name Application Allows the user to enter a first name, a space, and a last name Displays the name as last name, comma, space, first name Programming with Microsoft Visual Basic 2012

25 Accessing the Characters in a String (cont.)
The Rearrange Name Application (cont.) Figure 8-17 Interface showing the rearranged name Figure 8-16 btnRearrange_Click procedure Programming with Microsoft Visual Basic 2012

26 Using Pattern-Matching to Compare Strings
Like operator Allows the use of pattern-matching characters to determine whether one string is equal to another You must specify the string to be examined and the pattern to be matched The pattern can contain pattern-matching characters Returns a Boolean value True if a match is made False otherwise Programming with Microsoft Visual Basic 2012

27 Using Pattern-Matching to Compare Strings (cont.)
Figure 8-18 Syntax and examples of the Like operator (continues) Programming with Microsoft Visual Basic 2012

28 Using Pattern-Matching to Compare Strings (cont.)
(continued) Figure 8-18 Syntax and examples of the Like operator (continues) Programming with Microsoft Visual Basic 2012

29 Using Pattern-Matching to Compare Strings (cont.)
(continued) Figure 8-18 Syntax and examples of the Like operator Programming with Microsoft Visual Basic 2012

30 Using Pattern-Matching to Compare Strings (cont.)
Modifying the Product ID Application Figure 8-20 Product ID added to the list box Figure 8-19 Modified Click event procedure for the btnAdd control Programming with Microsoft Visual Basic 2012

31 Lesson A Summary Programming with Microsoft Visual Basic 2012
Figure 8-21 String manipulation techniques Programming with Microsoft Visual Basic 2012

32 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 Enable and disable a control Assign shortcut keys to commonly used menu items Code a menu item’s Click event procedure Include the Like operator in a procedure Programming with Microsoft Visual Basic 2012

33 Adding a Menu to a Form Menu strip control
Used to include one or more menus on a form A menu title appears on the menu bar at the top of the form Menu items can include: Commands Submenu items Separator bars Clicking a command on a menu executes it Clicking a submenu item opens an additional menu Separator bars provide visual grouping Programming with Microsoft Visual Basic 2012

34 Adding a Menu to a Form (cont.)
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 An ellipsis (…) after an item caption indicates a menu item requires more information The FILE menu should be the first item on the menu bar Cut, Copy, and Paste should appear on the EDIT menu Programming with Microsoft Visual Basic 2012

35 Adding a Menu to a Form (cont.)
Figure 8-22 Location of menu elements Programming with Microsoft Visual Basic 2012

36 Adding a Menu to a Form (cont.)
Figure 8-23 Six picture boxes selected in the interface Programming with Microsoft Visual Basic 2012

37 Adding a Menu to a Form (cont.)
Figure 8-24 Current status of the form Programming with Microsoft Visual Basic 2012

38 Adding a Menu to a Form (cont.)
Figure 8-25 MenuStrip control added to the form Programming with Microsoft Visual Basic 2012

39 Adding a Menu to a Form (cont.)
Figure 8-26 Menu title included on the form Figure 8-27 Drop-down list Figure 8-28 FILE menu opened during run time Programming with Microsoft Visual Basic 2012

40 Adding a Menu to a Form (cont.)
Assigning Shortcut Keys to Menu Items Shortcut keys Appear to the right of a menu item Allow the user to select an item without opening a 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 a menu is closed Programming with Microsoft Visual Basic 2012

41 Adding a Menu to a Form (cont.)
Assigning Shortcut Keys to Menu Items (cont.) Figure 8-30 Location of the shortcut keys on the menu Figure 8-29 Shortcut keys specified in the ShortcutKeys box Programming with Microsoft Visual Basic 2012

42 Adding a Menu to a Form (cont.)
Coding the Exit Menu Item How to end the Frankenstein Game application: The user clicks the Exit item on the FILE menu The Exit item’s Click event procedure calls Me.Close() Coding the txtLetter Control’s KeyPress Event Figure 8-31 txtLetter_KeyPress procedure Programming with Microsoft Visual Basic 2012

43 Lesson B Summary To add a MenuStrip control to a form:
Use the MenuStrip tool, which is located in the Menus & Toolbars section of the toolbox To create a menu: Replace the words “Type Here” with the menu element’s caption Assign a meaningful name and a unique access key to each menu element, with the exception of separator bars Programming with Microsoft Visual Basic 2012

44 Lesson B Summary (cont.)
To include a separator bar on a menu: Place your mouse pointer on a Type Here box and then click the list arrow that appears inside the box Click Separator on the list To enable/disable a control during run time: Set its Enabled property to True (enable) or False (disable) either in the Properties window or in code To assign shortcut keys to a menu item: Set the menu item’s ShortcutKeys property Programming with Microsoft Visual Basic 2012

45 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 Remove method in a procedure Include the Insert method in a procedure Include the Contains method in a procedure Programming with Microsoft Visual Basic 2012

46 Completing the Frankenstein Game Application
Figure 8-32 TOE chart for the Frankenstein Game application Programming with Microsoft Visual Basic 2012

47 Completing the Frankenstein Game Application (cont.)
Figure 8-33 Interface for the Frankenstein Game application from Lesson B Figure 8-34 Declaration statements for the class-level variables Programming with Microsoft Visual Basic 2012

48 Completing the Frankenstein Game Application (cont.)
Coding the FILE Menu’s New Game Option The mnuFileNew object’s Click event procedure is invoked when the user clicks the New Game option on the FILE menu Two ways to begin a new game: Click FILE on the menu bar, and then click New Game Press Ctrl+N Programming with Microsoft Visual Basic 2012

49 Completing the Frankenstein Game Application (cont.)
Coding the FILE Menu’s New Game Option (cont.) Figure 8-35 Pseudocode for the mnuFileNew_Click procedure Programming with Microsoft Visual Basic 2012

50 Completing the Frankenstein Game Application (cont.)
Coding the FILE Menu’s New Game Option (cont.) Figure 8-36 Two ways of determining whether the word contains five letters Programming with Microsoft Visual Basic 2012

51 Completing the Frankenstein Game Application (cont.)
Coding the FILE Menu’s New Game Option (cont.) Figure 8-37 Results of entering a valid word Programming with Microsoft Visual Basic 2012

52 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure Figure 8-38 Pseudocode for the btnCheck_Click, DisplayPicture, and DetermineGameOver procedures (continues) Programming with Microsoft Visual Basic 2012

53 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-38 Pseudocode for the btnCheck_Click, DisplayPicture, and DetermineGameOver procedures Programming with Microsoft Visual Basic 2012

54 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure (cont.) Figure 8-39 Additional code entered in the btnCheck_Click procedure Programming with Microsoft Visual Basic 2012

55 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure (cont.) Figure 8-40 Result of guessing the secret word Programming with Microsoft Visual Basic 2012

56 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure (cont.) Figure 8-41 Result of not guessing the secret word Programming with Microsoft Visual Basic 2012

57 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure (cont.) Figure 8-42 Frankenstein Game application’s code (continues) Programming with Microsoft Visual Basic 2012

58 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-42 Frankenstein Game application’s code (continues) Programming with Microsoft Visual Basic 2012

59 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-42 Frankenstein Game application’s code (continues) Programming with Microsoft Visual Basic 2012

60 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-42 Frankenstein Game application’s code (continues) Programming with Microsoft Visual Basic 2012

61 Completing the Frankenstein Game Application (cont.)
Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-42 Frankenstein Game application’s code Programming with Microsoft Visual Basic 2012

62 Lesson C Summary Length property: Determines the length of a string
Substring method: Accesses one or more characters in a string Like operator: Uses pattern-matching to compare two strings Remove method: Removes a specified number of characters located anywhere in a string Insert method: Inserts characters anywhere in a string Contains method: Determines whether a specific character is contained in a string Programming with Microsoft Visual Basic 2012


Download ppt "Chapter 8: String Manipulation"

Similar presentations


Ads by Google