Chapter 8: Manipulating Strings - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Chapter 8: Manipulating Strings

Description:

targetString: string targeted for character replacement ... Example: Ctrl S executes Save in MS Word. Assign shortcut keys to commonly used menu items ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 45
Provided by: course233
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8: Manipulating Strings


1
Chapter 8 Manipulating Strings
Programming with Microsoft Visual Basic 2005,
Third Edition
2
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

3
String ManipulationLesson A Objectives
(continued)
  • Search a string for one or more characters
  • Access characters contained in a string
  • Compare strings

4
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

5
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

6
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

7
Removing Characters from a String (continued)
Figure 8-4 Syntax, purpose, and examples of the
TrimStart, TrimEnd, and Trim methods (continued)
8
Removing Characters from a String (continued)
Figure 8-4 Syntax, purpose, and examples of the
TrimStart, TrimEnd, and Trim methods
9
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 xNameTextBoxs Text property

10
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

11
Replacing Characters in a String (continued)
Figure 8-6 Syntax, purpose, and examples of the
Replace method
12
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

13
The Mid Statement (continued)
Figure 8-7 Syntax, purpose, and examples of the
Mid statement
14
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)

15
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

16
The Insert Method (continued)
Figure 8-9 Syntax, purpose, and examples of the
Insert method
17
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)

18
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) 9999999
    If phone.Contains("(312)") Then
  • Condition will evaluate to True

19
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

20
The IndexOf Method (continued)
Figure 8-12 Syntax, purpose, and examples of the
IndexOf method (continued)
21
The IndexOf Method (continued)
Figure 8-12 Syntax, purpose, and examples of the
IndexOf method
22
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

23
Accessing Characters Contained in a String
(continued)
Figure 8-13 Syntax, purpose, and examples of the
Substring method
24
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 lt lowercase letters lt uppercase letters

25
Comparing Strings (continued)
Figure 8-14 Syntax, purpose, and examples of the
String.Compare method
26
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

27
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

28
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 items Click event procedure

29
Completing the Hangman Game Applications User
Interface
  • Objective create simplified version of Hangman
  • Lesson tasks
  • Complete the applications user interface
  • Begin coding the application

30
Completing the Hangman Game Applications User
Interface (continued)
Figure 8-16 Partially completed interface for
the Hangman Game application
31
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

32
Creating Menus (continued)
Figure 8-17 Location of menu elements
33
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

34
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

35
Assigning Shortcut Keys (continued)
Figure 8-23 Shortcut key appears next to the New
Game menu item
36
Coding 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()

37
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

38
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

39
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

40
Coding 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

41
Coding the xFileNewMenuItems Click Event
Procedure (continued)
Figure 8-26 Pseudocode for the
xFileNewMenuItems Click event procedure
(continued)
42
Coding the xFileNewMenuItems Click Event
Procedure (continued)
Figure 8-26 Pseudocode for the
xFileNewMenuItems Click event procedure
43
Coding the xFileNewMenuItems Click Event
Procedure (continued)
Figure 8-33 Result of guessing the word
44
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
Write a Comment
User Comments (0)
About PowerShow.com