1 / 34

Microsoft Visual Basic 2010: Reloaded Fourth Edition

Microsoft Visual Basic 2010: Reloaded Fourth Edition. Chapter Ten String Manipulation and Menus. Objectives. After studying this chapter, you should be able to: Determine the number of characters in a string Remove characters from a string Insert characters in a string Search a string

kfreese
Download Presentation

Microsoft Visual Basic 2010: Reloaded Fourth Edition

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. Microsoft Visual Basic 2010: ReloadedFourth Edition Chapter Ten String Manipulation and Menus

  2. Objectives After studying this chapter, you should be able to: • Determine the number of characters in a string • Remove characters from a string • Insert characters in a string • Search a string • Access the characters in a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  3. Objectives (cont’d.) • Align the characters in a string • Compare strings using pattern-matching • Add a menu to a form • Code a menu item’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  4. Working with Strings • Most applications need to manipulate string data in some way • String properties and methods are used to manipulate string data Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4

  5. Determining the Number of Characters in a String • Length property: stores the number of characters contained in a string as an integer value • Can be used with a String variable, a String named constant, or the Text property of a control Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5

  6. Figure 10-1: How to determine the number of characters in a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6

  7. Removing Characters from a String • Computer first makes a temporary copy of the string in memory and operates on the copy only • Trim method: removes one or more spaces from both the beginning and end of a string • Remove method: removes a specified number of characters located anywhere in a string • Index: an integer indicating the character’s position in the string • The first character in a string has an index of 0 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7

  8. Removing Characters from a String (cont’d.) • Arguments: • startIndex argument: the index of the first character to be removed • numCharsToRemove argument: number of characters to be removed • If omitted, all characters from the startIndex position through the end of the string are removed Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8

  9. Removing Characters from a String (cont’d.) Figure 10-2: How to remove characters from a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9

  10. Removing Characters from a String (cont’d.) Figure 10-2: How to remove characters from a string (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10

  11. Inserting Characters in a String • Insert method: used to insert characters anywhere within a string • Computer makes and operates on a temporary copy of the string • Arguments: • startIndex:specifies where in the string to insert the value • value:the character(s) to be inserted Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11

  12. Inserting Characters in a String (cont’d.) Figure 10-3: How to insert characters in a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12

  13. Searching a String • Containsmethod: • Determines if a string contains a specific sequence of characters • Returns a Boolean value of True when the substring is contained in the string, and False if not • Performs a case-sensitive search • Arguments: • subString: represents the sequence of characters to be searched for Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13

  14. Searching a String (cont’d.) • IndexOf method: returns an integer representing the location of a substring within a string • Performs a case-sensitive search • Arguments: • subString: sequence of characters to be searched for • startIndex: the starting position for the search (zero-relative) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14

  15. Figure 10-4: How to search a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15

  16. Figure 10-4: How to search a string (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16

  17. Accessing the Characters in a String • Substring method: accesses any number of characters contained in a string • Arguments: • startIndex: index of the first character to be accessed (zero-relative) • numCharsToAccess: number of characters to be accessed Microsoft Visual Basic 2010: Reloaded, Fourth Edition 17

  18. Accessing the Characters in a String (cont’d.) Figure 10-5: How to access characters in a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18

  19. Aligning the Characters in a String • PadLeft method: inserts characters at the beginning of a string • PadRight method: inserts characters at the end of a string • Arguments: • totalChars: represents the total number of characters you want in the resulting string • padCharacter: the character used to pad the string; default value is the space character Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19

  20. Figure 10-6: How to align the characters in a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition 20

  21. Using Pattern-Matching to Compare Strings • Like operator: • Uses pattern-matching characters to determine if one string is equal to another • Returns a Boolean value (True/False) • Arguments: • pattern: contains one or more pattern-matching characters • characterList: a listing of characters to be matched Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21

  22. Using Pattern-Matching to Compare Strings • Pattern-matching characters: • ? Represents 1 character only • * represents 0 or more characters • # represents a single digit • Use square brackets [ ] to provide a list of characters to match • Use a hyphen between characters to specify a range of characters Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22

  23. Figure 10-7: How to use pattern-matching to compare strings Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23

  24. Figure 10-7: How to use pattern-matching to compare strings (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24

  25. Adding a Menu to a Form • Menu strip control: used to include one or more menus on a Windows form • Found in the Menus & Toolbars section of the toolbox • Menu title: appears on the menu bar at the top of the form • When clicked, the menu opens and displays a list of options called menu items • Clicking a menu item executes the command associated with it Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25

  26. Adding a Menu to a Form (cont’d.) Figure 10-8: Location of menu elements Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26

  27. Adding a Menu to a Form (cont’d.) • Each menu element is considered an object • Each has a set of properties associated with it • Name property: used to refer to the menu element in code • Text property: stores the menu element’s caption – the text the user sees • Access key: used in combination with the Alt key, will open the menu • Shortcut key: allows the user to select the item without opening the menu Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27

  28. Adding a Menu to a Form (cont’d.) Figure 10-9: Game menu Figure 10-10: Exit command’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28

  29. Programming Tutorial 1 • Creating the Guess the Word Game Application Figure 10-12: MainForm in the Guess the Word Game application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29

  30. Programming Tutorial 2 • Creating the Bucky Burgers Application Figure 10-22: MainForm for the Bucky Burgers application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30

  31. Programming Example • Yolanda Drapery Application Figure 10-28: MainForm in the Yolanda Drapery application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31

  32. Summary • Use a menu strip control to add one or more menus to a form • Menu elements should have access keys • String manipulation techniques: • Length property: number of characters in the string • Trim method: removes leading and trailing spaces • Remove method: removes characters from a string • Insert method: inserts characters into a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32

  33. Summary (cont'd.) • String manipulation techniques (cont’d.): • Contains method: determines whether a specific sequence of characters appears in a string; returns a Boolean value • IndexOf method: determines whether a specific sequence of characters appears in a string; returns the integer position where the sequence starts • Substring method: accesses one or more characters in a string Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33

  34. Summary (cont'd.) • String manipulation techniques (cont’d.): • PadLeft method: pads the beginning of a string with the specified character • PadRight method: pads the end of a string with the specified character • Like operator: uses pattern-matching to compare strings Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34

More Related