Chapter 8: String Manipulation

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic th Edition
Advertisements

Using Macros and Visual Basic for Applications (VBA) with Excel
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Microsoft Office 2010 Access Chapter 1 Creating and Using a Database.
Chapter 8: Manipulating Strings
Chapter 7: Sub and Function Procedures
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
1.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Chapter Four The Selection Structure
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
Chapter 4: The Selection Structure
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 Working with Menus and Dialog Boxes. 2 Objectives You will be able to Create and edit menus for Windows Forms applications. Write code to handle menu.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Program Design and Coding
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Tutorial 11 Five windows included in the Visual Basic Startup Screen Main Form Toolbox Project Explorer (Project) Properties.
Key Applications Module Lesson 21 — Access Essentials
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 8: Manipulating Strings
Tutorial 91 Databases A database is an organized collection of related information stored in a file on a disk A database allows companies to store information.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 5: More on the Selection Structure
1.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Menu & Clipboard Menu Cut / Copy / Paste. Menus Created in the Menu Editor Can contain menu titles, menu items, separator bars, submenu titles, and submenu.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Chapter 4: Do-It-Yourself Designing (Designing Interfaces)
Excel Tutorial 8 Developing an Excel Application
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter 1: An Introduction to Visual Basic 2015
CIS16 Application Development and Programming using Visual Basic.net
String Manipulation and More Controls
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Chapter 8: String Manipulation

Previewing the Frankenstein Game Application Figure 8-1 Interface for the Frankenstein Game application Programming with Microsoft Visual Basic 2012

Previewing the Frankenstein Game Application (cont.) Figure 8-2 Interface after guessing the secret word Programming with Microsoft Visual Basic 2012

Previewing the Frankenstein Game Application (cont.) Figure 8-3 Interface after not guessing the secret word Programming with Microsoft Visual Basic 2012

Lesson A Objectives After studying Lesson A, you should be able to: Determine the number of characters in a string Remove characters from a string Insert characters in a string Align the characters in a string Search a string Access characters in a string Compare strings using pattern-matching Programming with Microsoft Visual Basic 2012

Determining the Number of Characters in a String Applications often need to manipulate string data There are two scenarios involving string manipulation: Determine the first character of an inventory part number Search an address to find a street name Length property Stores the number of characters contained in a string Syntax: string.Length Returns an integer value Programming with Microsoft Visual Basic 2012

Determining the Number of Characters in a String (cont.) Figure 8-4 Syntax and examples of the Length property Programming with Microsoft Visual Basic 2012

Removing Characters from a String Trim method Removes space characters from both ends of a string Remove method Removes a specified number of characters located anywhere in a string The computer makes a temporary copy of the string in memory, and then performs the specified removal on the copy The original string is not changed The modified copy is returned to the program Programming with Microsoft Visual Basic 2012

Removing Characters from a String (cont.) Figure 8-5 Syntax and examples of the Trim and Remove methods Programming with Microsoft Visual Basic 2012

Removing Characters from a String (cont.) The Product ID Application Figure 8-7 Sample run of the Product ID application Figure 8-6 btnAdd control’s Click event procedure Programming with Microsoft Visual Basic 2012

Inserting Characters in a String Insert method Inserts characters anywhere in a string The computer makes a temporary copy of a string and inserts specified characters into the copy Returns a string with the appropriate characters inserted startIndex argument An integer specifying the starting location to insert the value Represents the position in a string To insert the value at the beginning of a string, use a startIndex of 0 Programming with Microsoft Visual Basic 2012

Inserting Characters in a String (cont.) Figure 8-8 Syntax and examples of the Insert method Programming with Microsoft Visual Basic 2012

Inserting Characters in a String (cont.) Aligning the Characters in a String PadLeft method Inserts zero or more characters at the beginning of a string Results in a string of a specified length Right-aligns the characters within the string PadRight method Inserts characters at the end of the string Left-aligns the characters within the string Programming with Microsoft Visual Basic 2012

Inserting Characters in a String (cont.) Aligning the Characters in a String (cont.) Figure 8-9 Syntax and examples of the PadLeft and PadRight methods Programming with Microsoft Visual Basic 2012

Inserting Characters in a String (cont.) The Net Pay Application Allows the user to enter the amount of the employee’s net pay Displays the net pay with a leading dollar sign, asterisks, and two decimal places Uses the Insert and PadLeft methods Programming with Microsoft Visual Basic 2012

Inserting Characters in a String (cont.) The Net Pay Application (cont.) Figure 8-11 Interface showing the formatted net pay Figure 8-10 btnFormat_Click procedure Programming with Microsoft Visual Basic 2012

Searching a String Contains method Used to search a string to find a specific sequence of characters Returns a Boolean value True if found False otherwise You must specify the characters you are searching for The search begins with the first character in the string Programming with Microsoft Visual Basic 2012

Searching a String (cont.) IndexOf method Determines if a string contains a character sequence Returns an integer specifying the start position of a found character sequence (substring) If the substring is not found, the method returns -1 You must specify the sequence of characters to search for You may specify the index of the character at which to begin the search If not specified, the search begins with the first character in the string Programming with Microsoft Visual Basic 2012

Searching a String (cont.) Figure 8-12 Syntax and examples of the Contains and IndexOf methods (continues) Programming with Microsoft Visual Basic 2012

Searching a String (cont.) Figure 8-12 Syntax and examples of the Contains and IndexOf methods Programming with Microsoft Visual Basic 2012

Searching a String (cont.) The City and State Application Allows the user to enter a string Composed of a city name, a comma, a space, and a state name Displays index of the comma in the string Figure 8-14 Interface showing the comma’s index Figure 8-13 btnLocate_Click procedure Programming with Microsoft Visual Basic 2012

Accessing the Characters in a String Substring method Used to access any number of characters in a string Returns a string with a specified number of characters Specify the index of the first character to access in the string, and the number of characters to retrieve If the number of characters is not specified, the method returns all characters from the startIndex position to the end of the string Programming with Microsoft Visual Basic 2012

Accessing the Characters in a String (cont.) Figure 8-15 Syntax and examples of the Substring method Programming with Microsoft Visual Basic 2012

Accessing the Characters in a String (cont.) The Rearrange Name Application Allows the user to enter a first name, a space, and a last name Displays the name as last name, comma, space, first name Programming with Microsoft Visual Basic 2012

Accessing the Characters in a String (cont.) The Rearrange Name Application (cont.) Figure 8-17 Interface showing the rearranged name Figure 8-16 btnRearrange_Click procedure Programming with Microsoft Visual Basic 2012

Using Pattern-Matching to Compare Strings Like operator Allows the use of pattern-matching characters to determine whether one string is equal to another You must specify the string to be examined and the pattern to be matched The pattern can contain pattern-matching characters Returns a Boolean value True if a match is made False otherwise Programming with Microsoft Visual Basic 2012

Using Pattern-Matching to Compare Strings (cont.) Figure 8-18 Syntax and examples of the Like operator (continues) Programming with Microsoft Visual Basic 2012

Using Pattern-Matching to Compare Strings (cont.) (continued) Figure 8-18 Syntax and examples of the Like operator (continues) Programming with Microsoft Visual Basic 2012

Using Pattern-Matching to Compare Strings (cont.) (continued) Figure 8-18 Syntax and examples of the Like operator Programming with Microsoft Visual Basic 2012

Using Pattern-Matching to Compare Strings (cont.) Modifying the Product ID Application Figure 8-20 Product ID added to the list box Figure 8-19 Modified Click event procedure for the btnAdd control Programming with Microsoft Visual Basic 2012

Lesson A Summary Programming with Microsoft Visual Basic 2012 Figure 8-21 String manipulation techniques Programming with Microsoft Visual Basic 2012

Lesson B Objectives After studying Lesson B, you should be able to: Include a MenuStrip control on a form Add elements to a menu Assign access keys to menu elements Enable and disable a control Assign shortcut keys to commonly used menu items Code a menu item’s Click event procedure Include the Like operator in a procedure Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form Menu strip control Used to include one or more menus on a form A menu title appears on the menu bar at the top of the form Menu items can include: Commands Submenu items Separator bars Clicking a command on a menu executes it Clicking a submenu item opens an additional menu Separator bars provide visual grouping Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form (cont.) 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: Use book title capitalization for menu item captions An ellipsis (…) after an item caption indicates a menu item requires more information The FILE menu should be the first item on the menu bar Cut, Copy, and Paste should appear on the EDIT menu Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form (cont.) Figure 8-22 Location of menu elements Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form (cont.) Figure 8-23 Six picture boxes selected in the interface Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form (cont.) Figure 8-24 Current status of the form Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form (cont.) Figure 8-25 MenuStrip control added to the form Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form (cont.) Figure 8-26 Menu title included on the form Figure 8-27 Drop-down list Figure 8-28 FILE menu opened during run time Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form (cont.) Assigning Shortcut Keys to Menu Items Shortcut keys Appear to the right of a menu item Allow the user to select an item without opening a menu Example: Ctrl+X and Ctrl+V Cut and paste commands Assign shortcut keys to commonly used menu items Follow Windows standard conventions Shortcut keys can be used when a menu is closed Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form (cont.) Assigning Shortcut Keys to Menu Items (cont.) Figure 8-30 Location of the shortcut keys on the menu Figure 8-29 Shortcut keys specified in the ShortcutKeys box Programming with Microsoft Visual Basic 2012

Adding a Menu to a Form (cont.) Coding the Exit Menu Item How to end the Frankenstein Game application: The user clicks the Exit item on the FILE menu The Exit item’s Click event procedure calls Me.Close() Coding the txtLetter Control’s KeyPress Event Figure 8-31 txtLetter_KeyPress procedure Programming with Microsoft Visual Basic 2012

Lesson B Summary To add a MenuStrip control to a form: Use the MenuStrip tool, which is located in the Menus & Toolbars section of the toolbox To create a menu: Replace the words “Type Here” with the menu element’s caption Assign a meaningful name and a unique access key to each menu element, with the exception of separator bars Programming with Microsoft Visual Basic 2012

Lesson B Summary (cont.) To include a separator bar on a menu: Place your mouse pointer on a Type Here box and then click the list arrow that appears inside the box Click Separator on the list To enable/disable a control during run time: Set its Enabled property to True (enable) or False (disable) either in the Properties window or in code To assign shortcut keys to a menu item: Set the menu item’s ShortcutKeys property Programming with Microsoft Visual Basic 2012

Lesson C Objectives After studying Lesson C, you should be able to: Include the Length property in a procedure Include the Substring method in a procedure Include the Remove method in a procedure Include the Insert method in a procedure Include the Contains method in a procedure Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application Figure 8-32 TOE chart for the Frankenstein Game application Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Figure 8-33 Interface for the Frankenstein Game application from Lesson B Figure 8-34 Declaration statements for the class-level variables Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Coding the FILE Menu’s New Game Option The mnuFileNew object’s Click event procedure is invoked when the user clicks the New Game option on the FILE menu Two ways to begin a new game: Click FILE on the menu bar, and then click New Game Press Ctrl+N Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Coding the FILE Menu’s New Game Option (cont.) Figure 8-35 Pseudocode for the mnuFileNew_Click procedure Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Coding the FILE Menu’s New Game Option (cont.) Figure 8-36 Two ways of determining whether the word contains five letters Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Coding the FILE Menu’s New Game Option (cont.) Figure 8-37 Results of entering a valid word Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure Figure 8-38 Pseudocode for the btnCheck_Click, DisplayPicture, and DetermineGameOver procedures (continues) Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-38 Pseudocode for the btnCheck_Click, DisplayPicture, and DetermineGameOver procedures Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure (cont.) Figure 8-39 Additional code entered in the btnCheck_Click procedure Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure (cont.) Figure 8-40 Result of guessing the secret word Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure (cont.) Figure 8-41 Result of not guessing the secret word Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure (cont.) Figure 8-42 Frankenstein Game application’s code (continues) Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-42 Frankenstein Game application’s code (continues) Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-42 Frankenstein Game application’s code (continues) Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-42 Frankenstein Game application’s code (continues) Programming with Microsoft Visual Basic 2012

Completing the Frankenstein Game Application (cont.) Completing the Check Button’s Click Event Procedure (cont.) (continued) Figure 8-42 Frankenstein Game application’s code Programming with Microsoft Visual Basic 2012

Lesson C Summary Length property: Determines the length of a string Substring method: Accesses one or more characters in a string Like operator: Uses pattern-matching to compare two strings Remove method: Removes a specified number of characters located anywhere in a string Insert method: Inserts characters anywhere in a string Contains method: Determines whether a specific character is contained in a string Programming with Microsoft Visual Basic 2012