Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

44
Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition

Transcript of Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

Page 1: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

Chapter 8: Manipulating Strings

Programming with Microsoft Visual Basic 2005, Third Edition

Page 2: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

2Programming 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

Page 3: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

3Programming 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

Page 4: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

4Programming 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

Page 5: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

5Programming 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

Page 6: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

6Programming 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

Page 7: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

7Programming 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)

Page 8: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

8Programming 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

Page 9: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

9Programming 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

Page 10: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

10Programming 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

Page 11: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

11Programming with Microsoft Visual Basic 2005, Third Edition

Replacing Characters in a String (continued)

Figure 8-6: Syntax, purpose, and examples of the Replace method

Page 12: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

12Programming 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

Page 13: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

13Programming with Microsoft Visual Basic 2005, Third Edition

The Mid Statement (continued)

Figure 8-7: Syntax, purpose, and examples of the Mid statement

Page 14: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

14Programming 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])

Page 15: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

15Programming 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

Page 16: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

16Programming with Microsoft Visual Basic 2005, Third Edition

The Insert Method (continued)

Figure 8-9: Syntax, purpose, and examples of the Insert method

Page 17: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

17Programming 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)

Page 18: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

18Programming 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

Page 19: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

19Programming 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

Page 20: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

20Programming with Microsoft Visual Basic 2005, Third Edition

The IndexOf Method (continued)

Figure 8-12: Syntax, purpose, and examples of the IndexOf method (continued)

Page 21: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

21Programming with Microsoft Visual Basic 2005, Third Edition

The IndexOf Method (continued)

Figure 8-12: Syntax, purpose, and examples of the IndexOf method

Page 22: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

22Programming 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

Page 23: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

23Programming 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

Page 24: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

24Programming 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

Page 25: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

25Programming with Microsoft Visual Basic 2005, Third Edition

Comparing Strings (continued)

Figure 8-14: Syntax, purpose, and examples of the String.Compare method

Page 26: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

26Programming 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

Page 27: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

27Programming 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

Page 28: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

28Programming 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

Page 29: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

29Programming 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

Page 30: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

30Programming 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

Page 31: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

31Programming 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

Page 32: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

32Programming with Microsoft Visual Basic 2005, Third Edition

Creating Menus (continued)

Figure 8-17: Location of menu elements

Page 33: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

33Programming 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

Page 34: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

34Programming 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

Page 35: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

35Programming with Microsoft Visual Basic 2005, Third Edition

Assigning Shortcut Keys (continued)

Figure 8-23: Shortcut key appears next to the New Game menu item

Page 36: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

36Programming 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()

Page 37: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

37Programming 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

Page 38: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

38Programming with Microsoft Visual Basic 2005, Third Edition

Completing the Hangman Game Application

Lesson 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

Page 39: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

39Programming 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

Page 40: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

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

Page 41: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

41Programming 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)

Page 42: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

42Programming 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

Page 43: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

43Programming with Microsoft Visual Basic 2005, Third Edition

Coding the xFileNewMenuItem’s Click Event Procedure (continued)

Figure 8-33: Result of guessing the word

Page 44: Chapter 8: Manipulating Strings Programming with Microsoft Visual Basic 2005, Third Edition.

44Programming 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