Download presentation
Presentation is loading. Please wait.
Published byPeter Wiggins Modified over 9 years ago
1
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings
2
Tutorial 8: Manipulating Strings2 String Manipulation Lesson A Objectives After completing this lesson, you will be able to: Determine the number of characters contained in a string Remove characters from a string Determine whether a string begins or ends with one or more specific characters Access characters from the beginning, middle, and end of a string Replace one or more characters in a string Insert characters within a string Search a string for one or more characters
3
Tutorial 8: Manipulating Strings3 The Length Property of a String You can use a string’s Length property to determine the number of characters contained in the string Debug.WriteLine(Me.ZipTextBox.Text.Length) Dim strName As String = “Paul Blackfeather” Debug.WriteLine(strName.Length) Dim strPhone As String strPhone = InputBox(“10-digit phone number”, “Phone”) Do While strPhone.Length = 10 ‘instructions strPhone = InputBox(“10-digit phone number”, “Phone”) Loop
4
Tutorial 8: Manipulating Strings4 More String Manipulation Functions Remove(start, count)Deletes count characters from a string starting at start StartsWith(subString)Returns true if the string starts with the subString EndsWith(subString)Returns true if the string ends with the subString Substring(start[, count])Extracts a substring of count characters starting at start Replace(oldValue, newValue)Replaces all instances of oldValue with newValue Insert(start, value)Inserts value into a string starting at start IndexOf(value[, startIndex])Returns the index of where value is found starting at startIndex Mid(target, start [, count]) = replacementString replace a specified number of characters in target with characters from another string
5
Tutorial 8: Manipulating Strings5 Removing Characters from a String string.TrimRemoves leading and trailing spaces string.Trim(char)Removes leading and trailing char string.TrimStartRemoves leading spaces string.TrimStart(char)Removes leading char string.TrimEndRemoves trailing spaces string.TrimEnd(char)Removes trailing char
6
Tutorial 8: Manipulating Strings6 The Remove Method You can use the Remove method to remove one or more characters located anywhere in a string Figure 8-5 shows the syntax of the Remove method and includes several examples of using the method
7
Tutorial 8: Manipulating Strings7
8
8 Determining Whether a String Begins or Ends with a Specific Sequence of Characters Visual Basic.NET provides The StartWith method for determining whether a specific sequence of characters occurs at the beginning of a string The EndsWith method for determining whether a specific sequence of characters occurs at the end of a string
9
Tutorial 8: Manipulating Strings9 Accessing Characters Contained in a String You can use the Substring method to access any number of characters in a string The Substring method contains two arguments: startIndex and count StartIndex is the index of the first character you want to access in the string The count argument, which is optional, specifies the number of characters you want to access
10
Tutorial 8: Manipulating Strings10 Replacing Characters in a String You can use the Replace method to replace a sequence of characters in a string with another sequence of characters Or, you can use it to replace the dashes in a Social Security number with the empty string
11
Tutorial 8: Manipulating Strings11 Inserting Characters within a String You can use the Insert method to insert characters within a string For example, you can use the Insert method to insert an employee’s middle initial within his or her name
12
Tutorial 8: Manipulating Strings12 Searching a String You can use the IndexOf method to search a string to determine whether it contains a specific sequence of characters Figure 8-11 shows the syntax and several examples of the IndexOf method Also, examine Figure 8-12 in the textbook for more String manipulation techniques
13
Tutorial 8: Manipulating Strings13
14
Tutorial 8: Manipulating Strings14
15
Tutorial 8: Manipulating Strings15 The Mid Statement You can use the Mid statement to replace a specified number of characters in a string with characters from another string Figure 8-9 shows the syntax of the Mid statement and includes several examples of using the statement
16
Tutorial 8: Manipulating Strings16
17
Tutorial 8: Manipulating Strings17
18
Tutorial 8: Manipulating Strings18 String Conversion string.ToUpperConverts string to UPPER CASE string.ToLowerConverts string to lower case UCase(string)Converts string to UPPER CASE LCase(string)Converts string to lower case StrConv(string, format) Converts string according to format: vbStrConv.LowerCase vbStrConv.UpperCase vbStrConv.ProperCase
19
Tutorial 8: Manipulating Strings19 Using a Main Menu Control Lesson B Objectives After completing this lesson, you will be able to: Add a main menu control to a form Add main menu elements to a main menu control Assign access keys and shortcut keys to menu elements Code a menu item’s Click event procedure
20
Tutorial 8: Manipulating Strings20 Adding a Main Menu Control to a Form You use a main menu control to include one or more menus in an application Each menu contains a menu title, which appears on the menu bar at the top of a Windows form When you click a menu title, its corresponding menu opens and displays a list of options, called menu items As in all Windows applications, clicking a command on a menu executes the command, and clicking a submenu title opens an additional menu of options
21
Tutorial 8: Manipulating Strings21 Adding a Main Menu Control to a Form Each of the options on a submenu is referred to as a submenu item The purpose of a separator bar is to visually group together the related items on a menu or submenu Each menu element is considered an object and has a set of properties associated with it The most commonly used properties for a menu element are the Name and Text properties
22
Tutorial 8: Manipulating Strings22 Adding Shortcut Keys Shortcut keys appear to the right of a menu item and allow you to select an item without opening the menu
23
Tutorial 8: Manipulating Strings23 Completing the Hangman Game Application Lesson C Objectives After completing this lesson, you will be able to: Include the Substring method in a procedure Include the Mid statement in a procedure Include the IndexOf method in a procedure
24
Tutorial 8: Manipulating Strings24 Coding the Click Event Procedure for the FileNewMenuItem Each time the user wants to begin a new Hangman game, he or she will need to click File on the application’s menu bar and then click New Game Figure 8-23 shows the pseudocode for the FileNewMenuItem’s Click event procedure
25
Tutorial 8: Manipulating Strings25
26
Tutorial 8: Manipulating Strings26 Coding the Label Controls that Contain the Letters of the Alphabet The pseudocode shown in Figure 8-27 identifies the tasks to be performed by the Click event procedures for the 26 label controls
27
Tutorial 8: Manipulating Strings27
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.