Download - Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition
Chapter 8: Manipulating Strings
Programming with Microsoft Visual Basic 2005, Third Edition
2Programming with Microsoft Visual Basic 2005, Third Edition
String ManipulationLesson A Objectives
• Determine the number of characters contained in a string
• Remove characters from a string
• Replace one or more characters in a string
• Insert characters within a string
3Programming with Microsoft Visual Basic 2005, Third Edition
String ManipulationLesson A Objectives (continued)
• Search a string for one or more characters
• Access characters contained in a string
• Compare strings
4Programming with Microsoft Visual Basic 2005, Third Edition
Manipulating Strings in Visual Basic 2005
• Applications often need to manipulate string data
• Two scenarios involving string manipulation
– Determining the first letter of an inventory part id
– Validating last three characters in an employee id
5Programming with Microsoft Visual Basic 2005, Third Edition
Determining the Number of Characters Contained in a String
• Length property
– Stores number of characters contained in a string
• Syntax of the Length property: string.Length
• Using Length property to validate length of ZIP code– Dim numChars As Integer numChars
= Me.xZipTextBox.Text.Length
– Length of entered ZIP code is assigned to numChars
6Programming with Microsoft Visual Basic 2005, Third Edition
Removing Characters from a String
• TrimStart method
– Removes one or more characters from start of string
• TrimEnd method
– Removes one or more characters from end of string
• Trim method
– Removes one or more characters from both ends
• All methods take optional trimChars argument
7Programming with Microsoft Visual Basic 2005, Third Edition
Removing Characters from a String (continued)
Figure 8-4: Syntax, purpose, and examples of the TrimStart, TrimEnd, and Trim methods (continued)
8Programming with Microsoft Visual Basic 2005, Third Edition
Removing Characters from a String (continued)
Figure 8-4: Syntax, purpose, and examples of the TrimStart, TrimEnd, and Trim methods
9Programming with Microsoft Visual Basic 2005, Third Edition
The Remove Method
• Remove method
– Removes one or more characters anywhere in string
– Returns a string with appropriate characters removed
• Syntax: string.Remove(startIndex, count)
• Example– Dim fullName As String = "John Cober“
Me.xNameTextBox.Text = fullName.Remove(0, 5)– Assigns “Cober” to xNameTextBox’s Text property
10Programming with Microsoft Visual Basic 2005, Third Edition
Replacing Characters in a String
• Replace method
– Replace one sequence of characters with another
• Example: replace area code “800” with “877”
• Replace syntax: string.Replace(oldValue, newValue)
– oldValue: sequence of characters in string to replace
– newValue: the replacement characters
• Replace method returns a string with newValue
11Programming with Microsoft Visual Basic 2005, Third Edition
Replacing Characters in a String (continued)
Figure 8-6: Syntax, purpose, and examples of the Replace method
12Programming with Microsoft Visual Basic 2005, Third Edition
The Mid Statement
• Mid statement
– Replaces set of characters with another string
• Syntax: Mid(targetString, start [, count]) = replacementString
– targetString: string targeted for character replacement
– replacementString: contains replacement characters
– start: position of first character of the targetString
– count: number of characters to replace in targetString
13Programming with Microsoft Visual Basic 2005, Third Edition
The Mid Statement (continued)
Figure 8-7: Syntax, purpose, and examples of the Mid statement
14Programming with Microsoft Visual Basic 2005, Third Edition
Inserting Characters at the Beginning and End of a String
• PadLeft method
– Inserts padded characters at start of string
– Right-aligns the characters within the string
– Syntax: string.PadLeft(length[, character])
• PadRight method
– Inserts padded characters at the end of the string
– Left-aligns the characters within the string
– Syntax: string.PadRight(length[, character])
15Programming with Microsoft Visual Basic 2005, Third Edition
The Insert Method
• Insert method
– Inserts characters anywhere within a string
• Example: insert middle initial within employee name
• Syntax: string.Insert(startIndex, value)
– startIndex specifies position in string where value will be inserted
16Programming with Microsoft Visual Basic 2005, Third Edition
The Insert Method (continued)
Figure 8-9: Syntax, purpose, and examples of the Insert method
17Programming with Microsoft Visual Basic 2005, Third Edition
Searching a String
• StartsWith method
– Determine if sequence occurs at start of string
– Syntax: string.StartsWith(subString)
• EndsWith method
– Determine if sequence occurs at the end of a string
– Syntax: string.EndsWith(subString)
• Both methods return Boolean value (True or False)
18Programming with Microsoft Visual Basic 2005, Third Edition
The Contains Method
• Contains method– Determines if string contains a character sequence– Returns a Boolean value (True or False)
• Contains syntax: string.Contains(subString)– subString is the sequence to search for in string
• Example– Dim phone As String = "(312) 999–9999“
If phone.Contains("(312)") Then– Condition will evaluate to True
19Programming with Microsoft Visual Basic 2005, Third Edition
The IndexOf Method
• IndexOf method
– Determine if string contains a character sequence
– Returns integer specifying start position of substring
– If substring is not found, method returns -1
• Syntax: string.IndexOf(value[, startIndex])
– value: sequence of characters to search in the string
– startIndex: index of character at which search begins
20Programming with Microsoft Visual Basic 2005, Third Edition
The IndexOf Method (continued)
Figure 8-12: Syntax, purpose, and examples of the IndexOf method (continued)
21Programming with Microsoft Visual Basic 2005, Third Edition
The IndexOf Method (continued)
Figure 8-12: Syntax, purpose, and examples of the IndexOf method
22Programming with Microsoft Visual Basic 2005, Third Edition
Accessing Characters Contained in a String
• Substring method
– Used to access any number of characters in a string
– Returns string with specified number of characters
• Syntax: string.Substring(startIndex[, count])
– startIndex: index of first character to access in string
– count: specifies number of characters to access
23Programming with Microsoft Visual Basic 2005, Third Edition
Accessing Characters Contained in a String (continued)
Figure 8-13: Syntax, purpose, and examples of the Substring method
24Programming with Microsoft Visual Basic 2005, Third Edition
Comparing Strings
• String.Compare method– Used to compare two strings– Supplements the comparison operators– Returns three values: 1, 0, and -1
• Syntax: String.Compare(string1,string2[,ignoreCase])– string1 and string2 are two strings to compare– ignoreCase: Boolean argument set to True or False
• Word sort rules are used to compare strings– Numbers < lowercase letters < uppercase letters
25Programming with Microsoft Visual Basic 2005, Third Edition
Comparing Strings (continued)
Figure 8-14: Syntax, purpose, and examples of the String.Compare method
26Programming with Microsoft Visual Basic 2005, Third Edition
Summary – Lesson A
• Determine string length with Length property
• Methods for removing characters: Trim, TrimStart, TrimEnd, and Remove
• Replace characters with Replace method and Mid statement
• Pad strings with PadLeft and PadRight methods
• Insert characters in a string with the Insert method
27Programming with Microsoft Visual Basic 2005, Third Edition
Summary – Lesson A (continued)
• Methods determining presence of a substring: StartsWith, EndsWith, Contains, and IndexOf
• Access one or more characters in a string using the Substring method
• Compare strings using String.Compare method, Like operator, or comparison operators
28Programming with Microsoft Visual Basic 2005, Third Edition
Adding a Menu to a FormLesson B Objectives
• Include a MenuStrip control on a form
• Add elements to a menu
• Assign access keys to menu elements
• Assign shortcut keys to commonly used menu items
• Code a menu item’s Click event procedure
29Programming with Microsoft Visual Basic 2005, Third Edition
Completing the Hangman Game Application’s User Interface
• Objective: create simplified version of Hangman
• Lesson tasks
– Complete the application’s user interface
– Begin coding the application
30Programming with Microsoft Visual Basic 2005, Third Edition
Completing the Hangman Game Application’s User Interface
(continued)
Figure 8-16: Partially completed interface for the Hangman Game application
31Programming with Microsoft Visual Basic 2005, Third Edition
Creating Menus
• MenuStrip control: basis for adding menus
• MenuStrip tool: instantiates MenuStrip control
• Menu title: appears on menu bar at top of form
• Menu items
– Commands, submenu items, or separator bars
– Clicking a command on the menu executes it
– Clicking a submenu item opens an additional menu
32Programming with Microsoft Visual Basic 2005, Third Edition
Creating Menus (continued)
Figure 8-17: Location of menu elements
33Programming with Microsoft Visual Basic 2005, Third Edition
Creating Menus (continued)
• Menu title captions should be one word only
• Menu item captions can be from one to three words
• Assign unique access keys to menu titles and items
• Follow Windows menu conventions
– Ellipsis (…) after item caption indicates need for input
– File menu should be first menu on menu bar
– Cut, Copy, Paste should appear on an Edit menu
34Programming with Microsoft Visual Basic 2005, Third Edition
Assigning Shortcut Keys to Menu Items
• Shortcut keys
– Appear to the right of a menu item
– Allow you to select an item without opening a menu
• Example: Ctrl + S executes Save in MS Word
• Assign shortcut keys to commonly used menu items
35Programming with Microsoft Visual Basic 2005, Third Edition
Assigning Shortcut Keys (continued)
Figure 8-23: Shortcut key appears next to the New Game menu item
36Programming with Microsoft Visual Basic 2005, Third Edition
Coding the Exit Option’s Click Event Procedure
• How to end the Hangman Game application
– User clicks the Exit item on the File menu
– Exit item’s Click event procedure calls Me.close()
37Programming with Microsoft Visual Basic 2005, Third Edition
Summary – Lesson B
• MenuStrip control is the basis for a menu
• Menu items: commands, submenu items, separator bars
• Assign a unique access key to each menu element
• Assign shortcut keys to commonly used menu items
• Follow the Windows standard when creating menus
38Programming with Microsoft Visual Basic 2005, Third Edition
Completing the Hangman Game Application
Lesson C Objectives
• Include the Length property in a procedure
• Include the Substring method in a procedure
• Include the Mid statement in a procedure
• Include the Contains method in a procedure
39Programming with Microsoft Visual Basic 2005, Third Edition
The Hangman Game Application
• Application requirements
– Allow one student to enter a five-letter word
– Allow another student to guess word, letter by letter
• Two events can end the game
– Second student guesses all of the letters in the word
– Second student makes 10 incorrect guesses
40Programming with Microsoft Visual Basic 2005, Third Edition
Coding the xFileNewMenuItem’s Click Event Procedure
• Two ways to begin a new Hangman game
– Click File on the menu bar, then click New Game
– Press Ctrl + N
41Programming with Microsoft Visual Basic 2005, Third Edition
Coding the xFileNewMenuItem’s Click Event Procedure (continued)
Figure 8-26: Pseudocode for the xFileNewMenuItem’s Click event procedure (continued)
42Programming with Microsoft Visual Basic 2005, Third Edition
Coding the xFileNewMenuItem’s Click Event Procedure (continued)
Figure 8-26: Pseudocode for the xFileNewMenuItem’s Click event procedure
43Programming with Microsoft Visual Basic 2005, Third Edition
Coding the xFileNewMenuItem’s Click Event Procedure (continued)
Figure 8-33: Result of guessing the word
44Programming with Microsoft Visual Basic 2005, Third Edition
Summary – Lesson C
• Hangman application uses various methods to manipulate strings
• Substring method: used to access characters in a string
• Mid statement: replaces one character with another
• Contains method: determines whether a specific character is within a string