Title: Chapter 8: Manipulating Strings
1Chapter 8 Manipulating Strings
Programming with Microsoft Visual Basic 2005,
Third Edition
2String 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
3String ManipulationLesson A Objectives
(continued)
- Search a string for one or more characters
- Access characters contained in a string
- Compare strings
4Manipulating 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
5Determining 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
6Removing 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
7Removing Characters from a String (continued)
Figure 8-4 Syntax, purpose, and examples of the
TrimStart, TrimEnd, and Trim methods (continued)
8Removing Characters from a String (continued)
Figure 8-4 Syntax, purpose, and examples of the
TrimStart, TrimEnd, and Trim methods
9The 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 xNameTextBoxs Text property
10Replacing 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
11Replacing Characters in a String (continued)
Figure 8-6 Syntax, purpose, and examples of the
Replace method
12The 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
13The Mid Statement (continued)
Figure 8-7 Syntax, purpose, and examples of the
Mid statement
14Inserting 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)
15The 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
16The Insert Method (continued)
Figure 8-9 Syntax, purpose, and examples of the
Insert method
17Searching 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)
18The 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) 9999999
If phone.Contains("(312)") Then - Condition will evaluate to True
19The 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
20The IndexOf Method (continued)
Figure 8-12 Syntax, purpose, and examples of the
IndexOf method (continued)
21The IndexOf Method (continued)
Figure 8-12 Syntax, purpose, and examples of the
IndexOf method
22Accessing 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
23Accessing Characters Contained in a String
(continued)
Figure 8-13 Syntax, purpose, and examples of the
Substring method
24Comparing 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 lt lowercase letters lt uppercase letters
25Comparing Strings (continued)
Figure 8-14 Syntax, purpose, and examples of the
String.Compare method
26Summary 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
27Summary 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
28Adding 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 items Click event procedure
29Completing the Hangman Game Applications User
Interface
- Objective create simplified version of Hangman
- Lesson tasks
- Complete the applications user interface
- Begin coding the application
30Completing the Hangman Game Applications User
Interface (continued)
Figure 8-16 Partially completed interface for
the Hangman Game application
31Creating 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
32Creating Menus (continued)
Figure 8-17 Location of menu elements
33Creating 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
34Assigning 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
35Assigning Shortcut Keys (continued)
Figure 8-23 Shortcut key appears next to the New
Game menu item
36Coding the Exit Options Click Event Procedure
- How to end the Hangman Game application
- User clicks the Exit item on the File menu
- Exit items Click event procedure calls Me.close()
37Summary 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
38Completing the Hangman Game ApplicationLesson 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
39The 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
40Coding the xFileNewMenuItems Click Event
Procedure
- Two ways to begin a new Hangman game
- Click File on the menu bar, then click New Game
- Press Ctrl N
41Coding the xFileNewMenuItems Click Event
Procedure (continued)
Figure 8-26 Pseudocode for the
xFileNewMenuItems Click event procedure
(continued)
42Coding the xFileNewMenuItems Click Event
Procedure (continued)
Figure 8-26 Pseudocode for the
xFileNewMenuItems Click event procedure
43Coding the xFileNewMenuItems Click Event
Procedure (continued)
Figure 8-33 Result of guessing the word
44Summary 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