440 likes | 453 Views
Chapter 8: Manipulating Strings. Programming with Microsoft Visual Basic 2005, Third Edition. String Manipulation Lesson A Objectives. Determine the number of characters contained in a string Remove characters from a string Replace one or more characters in a string
E N D
Chapter 8: Manipulating Strings Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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) Programming 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 Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
Replacing Characters in a String (continued) Figure 8-6: Syntax, purpose, and examples of the Replace method Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
The Mid Statement (continued) Figure 8-7: Syntax, purpose, and examples of the Mid statement Programming 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]) Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
The Insert Method (continued) Figure 8-9: Syntax, purpose, and examples of the Insert method Programming 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) Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
The IndexOf Method (continued) Figure 8-12: Syntax, purpose, and examples of the IndexOf method (continued) Programming with Microsoft Visual Basic 2005, Third Edition
The IndexOf Method (continued) Figure 8-12: Syntax, purpose, and examples of the IndexOf method Programming 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 Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
Comparing Strings (continued) Figure 8-14: Syntax, purpose, and examples of the String.Compare method Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
Creating Menus (continued) Figure 8-17: Location of menu elements Programming 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 Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
Assigning Shortcut Keys (continued) Figure 8-23: Shortcut key appears next to the New Game menu item Programming 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() Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
Completing 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 Programming 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 Programming 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 Programming 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) Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition
Coding the xFileNewMenuItem’s Click Event Procedure (continued) Figure 8-33: Result of guessing the word Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition