Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
-
Upload
philip-burns -
Category
Documents
-
view
216 -
download
0
Transcript of Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Programming with Microsoft Visual Basic 2008
Fourth Edition
Chapter Eight
String Manipulation
Working with Strings
• Applications often need to manipulate string data
• Two scenarios involving string manipulation – Determine first letter of an inventory part id– Search an address to find street name
2Programming with Microsoft Visual Basic 2008, Fourth Edition
Determining the Number of Characters in a String
• Length property:– Stores number of characters contained in string
• Syntax: string.Length• Returns integer value
3Programming with Microsoft Visual Basic 2008, Fourth Edition
Removing Spaces from the Beginning and End of a String
• Trim method:– Removes spaces from both ends of string
• Computer makes temporary copy of string in memory, then performs trimming on copy– Original string is not changed– Modified copy is returned to program
4Programming with Microsoft Visual Basic 2008, Fourth Edition
Replacing Characters in a String
• Replace method:– Replaces one sequence of characters with another
• Example: Replace area code “800” with “877”
• Must specify both sequence of characters in string to be replaced, and replacement characters to be used
• Computer makes temporary copy of string and replaces specified characters in copy
• Returns string that includes replaced characters
5Programming with Microsoft Visual Basic 2008, Fourth Edition
Replacing Characters in a String (continued)
6Programming with Microsoft Visual Basic 2008, Fourth EditionFigure 8-6: Syntax, purpose, and examples of the Replace method
The Mid Statement
• Mid statement: – Replaces set of characters with another string
• Must specify:– targetString: String targeted for character
replacement– replacementString: Contains replacement
characters to be used– start: Position of first character of targetString
where replacement should take place– count: Number of characters to replace in
targetString
7Programming with Microsoft Visual Basic 2008, Fourth Edition
The Mid Statement (continued)
8Programming with Microsoft Visual Basic 2008, Fourth Edition
Figure 8-7: Syntax, purpose, and examples of the Mid statement
Inserting Characters in a String
• PadLeft method:– Inserts padded characters at start of string – Right-aligns characters within string
• PadRight method:– Inserts padded characters at end of string– Left-aligns characters within string
• Must specify character to pad with, and desired final total length of target string– If no character is specified, space is used
9Programming with Microsoft Visual Basic 2008, Fourth Edition
Insert Method
• Insert method:– Inserts characters anywhere within string
• Examples: – Insert middle initial within employee name– Insert parentheses around area code in phone
number
• Must specify position (index) where new characters will be inserted
10Programming with Microsoft Visual Basic 2008, Fourth Edition
Insert Method (continued)
11Programming with Microsoft Visual Basic 2008, Fourth Edition
Figure 8-11: Syntax, purpose, and example of the Insert method
Accessing Characters Contained in a String
• Substring method: – Used to access any number of characters in string– Returns string with specified number of characters
• Must specify index of first character to access in string, and number of characters to retrieve– If number of characters is not specified, all
characters from start position to the end of string are returned
12Programming with Microsoft Visual Basic 2008, Fourth Edition
Accessing Characters Contained in a String (continued)
13Programming with Microsoft Visual Basic 2008, Fourth Edition
Figure 8-18: Syntax, purpose, and examples of the Substring methodc
Using Pattern-Matching to Compare Strings
• Like operator:– Allows use of pattern-matching characters to
determine whether one string is equal to another
• Must specify string to be examined and pattern to be matched– Pattern can contain pattern-matching characters
• Returns Boolean value– Returns True if match is made, False otherwise
14Programming with Microsoft Visual Basic 2008, Fourth Edition
Using Pattern-Matching to Compare Strings (continued)
15Programming with Microsoft Visual Basic 2008, Fourth Edition
Figure 8-21: Syntax, purpose, and examples of the Like operator
Adding a Menu to a Form
• MenuStrip control: Used to include one or more menus in application
• Menu title: Appears on menu bar at top of form
• Menu items can include:– Commands, submenu items, or separator bars
• Clicking command on menu executes it
• Clicking submenu item opens additional menu
• Separator bars provides visual grouping
16Programming with Microsoft Visual Basic 2008, Fourth Edition
Adding a Menu to a Form (continued)
17Programming with Microsoft Visual Basic 2008, Fourth Edition
Figure 8-24: Location of menu elements
Adding a Menu to a Form (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 standards:– Ellipsis (…) after item caption indicates dialog box
will display for user input – File menu should be first item on menu bar– Cut, Copy, Paste should appear on Edit menu
18Programming with Microsoft Visual Basic 2008, Fourth Edition
Assigning Shortcut Keys to Menu Items
• Shortcut keys:– Appear to right of menu item– Allow you to select item without opening menu
• Example: Ctrl+S executes Save in MS Word
• Assign shortcut keys to commonly used menu items– Follow Windows standard conventions
• Shortcut keys can be used when menu is closed
19Programming with Microsoft Visual Basic 2008, Fourth Edition
Assigning Shortcut Keys to Menu Items (continued)
20Programming with Microsoft Visual Basic 2008, Fourth Edition
Figure 8-29: Shortcut key specified in the ShortcutKeys box
Assigning Shortcut Keys to Menu Items (continued)
21Programming with Microsoft Visual Basic 2008, Fourth Edition
Figure 8-30: Location of the shortcut key on the menu