1 / 41

Chapter 8: Manipulating Strings

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

godfrey
Download Presentation

Chapter 8: Manipulating Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic .NET, Second Edition

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. Removing Characters from a String (continued) Programming with Microsoft Visual Basic .NET, Second Edition

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. Searching a String (continued) Figure 8-13: String manipulation techniques Programming with Microsoft Visual Basic .NET, Second Edition

  22. Searching a String (continued) Figure 8-13: String manipulation techniques (continued) Programming with Microsoft Visual Basic .NET, Second Edition

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. Adding a Main Menu Control to a Form (continued) Figure 8-15: Location of menu elements Programming with Microsoft Visual Basic .NET, Second Edition

  29. 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

  30. 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

  31. Assigning Shortcut Keys (continued) Figure 8-19: Shortcut key displayed on the File menu Programming with Microsoft Visual Basic .NET, Second Edition

  32. 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

  33. 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

  34. 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

  35. The Hangman Game Application (continued) Figure 8-22: Hangman Game application’s user interface Programming with Microsoft Visual Basic .NET, Second Edition

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

More Related