Download presentation
Presentation is loading. Please wait.
Published byCory Flowers Modified over 9 years ago
1
Microsoft Visual Basic 2010: Reloaded Fourth 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 Edition4
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 Edition5
6
Figure 10-1: How to determine the number of characters in a string 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 Edition7
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 Edition8
9
Figure 10-2: How to remove characters from a string 9 Removing Characters from a String (cont’d.)
10
Microsoft Visual Basic 2010: Reloaded, Fourth Edition Figure 10-2: How to remove characters from a string (cont’d.) 10 Removing Characters from a String (cont’d.)
11
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 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 11
12
Microsoft Visual Basic 2010: Reloaded, Fourth Edition12 Figure 10-3: How to insert characters in a string Inserting Characters in a String (cont’d.)
13
Searching a String Contains method: –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 Edition13
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 Edition14
15
Microsoft Visual Basic 2010: Reloaded, Fourth Edition15 Figure 10-4: How to search a string
16
Microsoft Visual Basic 2010: Reloaded, Fourth Edition16 Figure 10-4: How to search a string (cont’d.)
17
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 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 17
18
Microsoft Visual Basic 2010: Reloaded, Fourth Edition18 Figure 10-5: How to access characters in a string Accessing the Characters in a String (cont’d.)
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 Edition19
20
Microsoft Visual Basic 2010: Reloaded, Fourth Edition20 Figure 10-6: How to align the characters in a string
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 Edition21
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 Edition22
23
Microsoft Visual Basic 2010: Reloaded, Fourth Edition23 Figure 10-7: How to use pattern- matching to compare strings
24
Microsoft Visual Basic 2010: Reloaded, Fourth Edition24 Figure 10-7: How to use pattern-matching to compare strings (cont'd.)
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 Edition25
26
Adding a Menu to a Form (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition Figure 10-8: Location of menu elements 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 Edition27
28
Microsoft Visual Basic 2010: Reloaded, Fourth Edition Adding a Menu to a Form (cont’d.) 28 Figure 10-9: Game menu Figure 10-10: Exit command’s Click event procedure
29
Creating the Guess the Word Game Application Microsoft Visual Basic 2010: Reloaded, Fourth Edition Programming Tutorial 1 29 Figure 10-12: MainForm in the Guess the Word Game application
30
Creating the Bucky Burgers Application Microsoft Visual Basic 2010: Reloaded, Fourth Edition Programming Tutorial 2 30 Figure 10-22: MainForm for the Bucky Burgers application
31
Microsoft Visual Basic 2010: Reloaded, Fourth Edition Programming Example 31 Yolanda Drapery Application Figure 10-28: MainForm in the Yolanda Drapery application
32
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 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 32
33
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 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 33
34
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 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 34
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.