270 likes | 452 Views
Tutorial 8 Manipulating Strings. String Manipulation Lesson A Objectives. After completing this lesson, you will be able to: Determine the number of characters contained in a string Remove characters from a string Determine whether a string begins or ends with one or more specific characters
E N D
String ManipulationLesson A Objectives After completing this lesson, you will be able to: • Determine the number of characters contained in a string • Remove characters from a string • Determine whether a string begins or ends with one or more specific characters • Access characters from the beginning, middle, and end of a string • Replace one or more characters in a string • Insert characters within a string • Search a string for one or more characters
The Length Property of a String • In many applications, it is necessary to determine the number of characters contained in a string • You can use a string’s Length property to determine the number of characters contained in the string
The Remove Method • You can use the Remove method to remove one or more characters located anywhere in a string • Figure 8-5 shows the syntax of the Remove method and includes several examples of using the method
Determining Whether a String Begins or Ends with a Specific Sequence of Characters Visual Basic .NET provides • The StartWith method for determining whether a specific sequence of characters occurs at the beginning of a string • The EndsWith method for determining whether a specific sequence of characters occurs at the end of a string
Accessing CharactersContained in a String • You can use the Substring method to access any number of characters in a string • The Substring method contains two arguments: startIndex and count • StartIndex is the index of the first character you want to access in the string • The count argument, which is optional, specifies the number of characters you want to access
Replacing Characters in a String • You can use the Replace method to replace a sequence of characters in a string with another sequence of characters • Or, you can use it to replace the dashes in a Social Security number with the empty string
The Mid Statement • You can use the Mid statement to replace a specified number of characters in a string with characters from another string • Figure 8-9 shows the syntax of the Mid statement and includes several examples of using the statement
Inserting Characters within a String • You can use the Insert method to insert characters within a string • For example, you can use the Insert method to insert an employee’s middle initial within his or her name
Searching a String • You can use the IndexOf method to search a string to determine whether it contains a specific sequence of characters • Figure 8-11 shows the syntax and several examples of the IndexOf method • Also, examine Figure 8-12 in the textbook for more String manipulation techniques
Using a Main Menu ControlLesson B Objectives After completing this lesson, you will be able to: • Add a main menu control to a form • Add main menu elements to a main menu control • Assign access keys and shortcut keys to menu elements • Code a menu item’s Click event procedure
Adding a Main Menu Control to a Form • You use a mainmenu control to include one or more menus in an application • Each menu contains a menu title, which appears on the menu bar at the top of a Windows form • When you click a menu title, its corresponding menu opens and displays a list of options, called menu items • As in all Windows applications, clicking a command on a menu executes the command, and clicking a submenu title opens an additional menu of options
Adding a Main Menu Control to a Form • Each of the options on a submenu is referred to as a submenu item • The purpose of a separator bar is to visually group together the related items on a menu or submenu • Each menu element is considered an object and has a set of properties associated with it • The most commonly used properties for a menu element are the Name and Text properties
Adding Shortcut Keys • Shortcut keys appear to the right of a menu item and allow you to select an item without opening the menu
Completing the Hangman Game ApplicationLesson C Objectives After completing this lesson, you will be able to: • Include the Substring method in a procedure • Include the Mid statement in a procedure • Include the IndexOf method in a procedure
Coding the Click Event Procedure for the FileNewMenuItem • Each time the user wants to begin a new Hangman game, he or she will need to click File on the application’s menu bar and then click New Game • Figure 8-23 shows the pseudocode for the FileNewMenuItem’s Click event procedure
Coding the Label Controls thatContain the Letters of the Alphabet • The pseudocode shown in Figure 8-27 identifies the tasks to be performed by the Click event procedures for the 26 label controls