410 likes | 615 Views
Chapter 8: Manipulating Strings. Programming with Microsoft Visual Basic .NET, Second Edition. String Manipulation Lesson A Objectives. Determine the number of characters contained in a string Remove characters from a string
E N D
Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic .NET, Second Edition
String ManipulationLesson A Objectives • 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 Programming with Microsoft Visual Basic .NET, Second Edition
String ManipulationLesson A Objectives (continued) • 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 Programming with Microsoft Visual Basic .NET, Second Edition
Manipulating Strings in Visual Basic .NET • Many times, an application needs to manipulate (process) string data • For example, an application might need to: • Verify that an inventory part number begins with a specific letter • Determine whether the last three characters in an employee number are valid Programming with Microsoft Visual Basic .NET, Second Edition
Determining the Number of Characters Contained in a String • In many applications, it is necessary to determine the number of characters contained in a string • Use a string’s Length property to determine the number of characters contained in the string • Syntax of the Length property: string.Length Programming with Microsoft Visual Basic .NET, Second Edition
Removing Characters from a String • TrimStart method • Remove one or more characters from the beginning of a string • Syntax: string.TrimStart([trimChars]) • TrimEnd method • Remove one or more characters from the end of a string • Syntax: string.TrimEnd([trimChars]) Programming with Microsoft Visual Basic .NET, Second Edition
Removing Characters from a String (continued) • Trim method • Remove one or more characters from both the beginning and end of a string • Syntax: string.Trim([trimChars]) Programming with Microsoft Visual Basic .NET, Second Edition
Removing Characters from a String (continued) Programming with Microsoft Visual Basic .NET, Second Edition
The Remove Method • Use the Remove method to remove one or more characters located anywhere in a string • The Remove method returns a string with the appropriate characters removed • Syntax: string.Remove(startIndex, count) Programming with Microsoft Visual Basic .NET, Second Edition
Determining Whether a String Begins or Ends with a SpecificSequence of Characters • StartsWith method • Determine whether a specific sequence of characters occurs at the beginning of a string • Syntax: string.StartsWith(subString) • EndsWith method • Determine whether a specific sequence of characters occurs at the end of a string • Syntax: string.EndsWith(subString) Programming with Microsoft Visual Basic .NET, Second Edition
Accessing Characters Contained in a String • Use the Substring method to access any number of characters in a string • Syntax: string.Substring(startIndex[, count]) • startIndex: the index of the first character you want to access in the string • count (optional): specifies the number of characters you want to access Programming with Microsoft Visual Basic .NET, Second Edition
Replacing Characters in a String • Use Replace to replace a sequence of characters in a string with another sequence of characters • For example: • Replace area code “800” with area code “877” in a phone number • Replace the dashes in a Social Security number with the empty string Programming with Microsoft Visual Basic .NET, Second Edition
Replacing Characters in a String (continued) • Syntax: string.Replace(oldValue, newValue) • oldValue is the sequence of characters that you want to replace in the string • newValue is the replacement characters Programming with Microsoft Visual Basic .NET, Second Edition
The Mid Statement • Use the Mid statement to replace a specified number of characters in a string with characters from another string • Syntax:Mid(targetString, start [, count]) = replacementString • targetString: the string in which you want characters replaced Programming with Microsoft Visual Basic .NET, Second Edition
The Mid Statement (continued) • replacementString: contains the replacement characters • start: the character position of the first character you want replaced in the targetString • count: the number of characters to replace in the targetString Programming with Microsoft Visual Basic .NET, Second Edition
Inserting Characters at the Beginning and End of a String • Use the PadLeft and PadRight methods to pad a string with a character until the string is a specified length • PadLeft method pads the string on the left • Inserts the padded characters at the beginning of the string and right-aligns the characters in the string • Syntax: string.PadLeft(length[, character]) Programming with Microsoft Visual Basic .NET, Second Edition
Inserting Characters at the Beginning and End of a String (continued) • PadRight method pads the string on the right • Inserts the padded characters at the end of the string and left-aligns the characters in the string • Syntax: string.PadRight(length[, character]) Programming with Microsoft Visual Basic .NET, Second Edition
Inserting Characters within a String • Use the Insert method to insert characters within a string • For example: insert an employee’s middle initial within his or her name • Syntax: string.Insert(startIndex, value) • startIndex specifies where in the string you want the value inserted Programming with Microsoft Visual Basic .NET, Second Edition
Searching a String • Use the IndexOf method to search a string to determine whether it contains a specific sequence of characters • Syntax: string.IndexOf(value[, startIndex]) • value:the sequence of characters for which you are searching in the string • startIndex:the index of the character at which the search should begin Programming with Microsoft Visual Basic .NET, Second Edition
Searching a String (continued) • For example: • Determine if the area code “312” appears in a phone number • Determine if the street name “Elm Street” appears in an address Programming with Microsoft Visual Basic .NET, Second Edition
Searching a String (continued) Figure 8-13: String manipulation techniques Programming with Microsoft Visual Basic .NET, Second Edition
Searching a String (continued) Figure 8-13: String manipulation techniques (continued) Programming with Microsoft Visual Basic .NET, Second Edition
Using a Main Menu ControlLesson B Objectives • Add a main menu control to a form • Add menu elements to a main menu control • Assign access keys and shortcut keys to menu elements • Code a menu item’s Click event procedure Programming with Microsoft Visual Basic .NET, Second Edition
Completing the Hangman Game Application’s User Interface • You will create a simplified version of the Hangman game for Mr. Mitchell, who teaches second grade at Hinsbrook School • In this lesson, you will: • Complete the application’s user interface • Begin coding the application Programming with Microsoft Visual Basic .NET, Second Edition
Completing the Hangman Game Application’s User Interface (continued) Figure 8-14: Partially completed interface for the Hangman Game application Programming with Microsoft Visual Basic .NET, Second Edition
Adding a Main Menu Control to a Form • Use a main menu 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 Programming with Microsoft Visual Basic .NET, Second Edition
Adding a Main Menu Control to a Form (continued) • The menu items can be commands, separator bars, or submenu titles • Clicking a command on a menu executes the command • Clicking a submenu title opens an additional menu of options Programming with Microsoft Visual Basic .NET, Second Edition
Adding a Main Menu Control to a Form (continued) Figure 8-15: Location of menu elements Programming with Microsoft Visual Basic .NET, Second Edition
Adding a Main Menu Control to a Form (continued) • Each option 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 Programming with Microsoft Visual Basic .NET, Second Edition
Assigning Shortcut Keys • Shortcut keys • Appear to the right of a menu item • Allow you to select an item without opening the menu • You should assign shortcut keys only to commonly used menu items Programming with Microsoft Visual Basic .NET, Second Edition
Assigning Shortcut Keys (continued) Figure 8-19: Shortcut key displayed on the File menu Programming with Microsoft Visual Basic .NET, Second Edition
Coding the Click Event Procedure for the Exit Menu Item • When the user clicks the Exit item on the File menu, the item’s Click event procedure should end the Hangman Game application Programming with Microsoft Visual Basic .NET, Second Edition
Completing the Hangman Game ApplicationLesson C Objectives • Include the Substring method in a procedure • Include the Mid statement in a procedure • Include the IndexOf method in a procedure Programming with Microsoft Visual Basic .NET, Second Edition
The Hangman Game Application • An application that two students can use to play a simplified version of the Hangman game on the computer • The application should allow one of the students to enter a five-letter word, and then allow the other student to guess the word, letter by letter • The game is over when the second student either guesses all of the letters in the word or makes 10 incorrect guesses, whichever comes first Programming with Microsoft Visual Basic .NET, Second Edition
The Hangman Game Application (continued) Figure 8-22: Hangman Game application’s user interface Programming with Microsoft Visual Basic .NET, Second Edition
Coding the Click Event Procedure for the uiFileNewMenuItem • 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 • Click New Game Programming with Microsoft Visual Basic .NET, Second Edition
Coding the Click Event Procedure for the uiFileNewMenuItem (continued) Figure 8-24: Pseudocode for the uiFileNewMenuItem’s Click event procedure Programming with Microsoft Visual Basic .NET, Second Edition
Coding the Click Event Procedure for the uiFileNewMenuItem (continued) Figure 8-24: Pseudocode for the uiFileNewMenuItem’s Click event procedure (continued) Programming with Microsoft Visual Basic .NET, Second Edition
Summary • To determine the number of characters contained in a string, use the Length property in the following syntax: string.Length • To remove one or more characters from anywhere in a string, use the Remove method • Use the StartsWith method to determine whether a string begins with a specific sequence of characters Programming with Microsoft Visual Basic .NET, Second Edition
Summary (continued) • Use the EndsWith method to determine whether a string ends with a specific sequence of characters • To access one or more characters contained in a string, use the Substring method • To insert characters within a string, use the Insert method Programming with Microsoft Visual Basic .NET, Second Edition
Summary (continued) • To search a string to determine whether it contains a specific sequence of characters, use the IndexOf method • To add a main menu control to a form, use the MainMenu tool in the toolbox • To assign a shortcut key to a menu item, set the menu item’s Shortcut property • To replace a character in a string with another character, use the Mid statement Programming with Microsoft Visual Basic .NET, Second Edition