Presentation is loading. Please wait.

Presentation is loading. Please wait.

Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.

Similar presentations


Presentation on theme: "Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section."— Presentation transcript:

1 Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section

2 Clearly Visual Basic: Programming with Visual Basic 2008 Objectives Determine the number of characters in a string Remove spaces from the beginning and end of a string Replace characters in a string Insert characters in a string Search a string Access characters in a string Compare strings using pattern-matching 2

3 Clearly Visual Basic: Programming with Visual Basic 2008 Working with Strings Many times: –An application will need to manipulate (process) string data in some way The first string manipulation technique in this chapter –Shows you how to determine the number of characters in a string 3

4 Clearly Visual Basic: Programming with Visual Basic 2008 How Many Characters Are There? If an application expects the user to enter a seven- digit phone number: –Application’s code should verify that the user entered the required number of characters Number of characters contained in a string –Stored in the string’s Length property Value stored in the property is an integer 4

5 Clearly Visual Basic: Programming with Visual Basic 20085

6 Get Rid of Those Spaces Trim method –Used to remove (trim) any spaces from both the beginning and end of a string –Does not remove any characters from the original string –Returns a string that excludes any leading or trailing spaces 6

7 Clearly Visual Basic: Programming with Visual Basic 20087

8 The Product ID Application Displays (in a label control): –A listing of the product IDs entered by the user Each product ID must contain exactly five characters 8

9 Clearly Visual Basic: Programming with Visual Basic 2008 Let’s Make a Substitution Replace method –Used to replace a sequence of characters in a string with another sequence of character Figure 24-4 –oldValue The sequence of characters that you want to replace in the string –newValue The replacement characters 9

10 Clearly Visual Basic: Programming with Visual Basic 200810

11 Clearly Visual Basic: Programming with Visual Basic 2008 I Need To Fit This in Somewhere Insert method –Used to insert characters –Examples Insert an employee’s middle initial within his or her name Insert parentheses around the area code in a phone number 11

12 Clearly Visual Basic: Programming with Visual Basic 200812

13 Clearly Visual Basic: Programming with Visual Basic 2008 The Phone Numbers Application Saves (in a sequential access file): –The phone numbers entered by the user Each phone number is entered using: –12 characters in the following format: 111-222-3333 Before writing a phone number to the file: –The application removes the hyphens and then verifies that the phone number contains 10 characters Figures 24-8 and 24-9 –Show code and resulting display for formatted numbers 13

14 Clearly Visual Basic: Programming with Visual Basic 200814

15 Clearly Visual Basic: Programming with Visual Basic 2008 Where Does it Begin? IndexOf method –Used to search a string to determine whether it contains a specific sequence of characters –Example Determining whether the area code “(312)” appears in a phone number 15

16 Clearly Visual Basic: Programming with Visual Basic 200816

17 Clearly Visual Basic: Programming with Visual Basic 2008 I Just Want a Part of It Substring method –Used to access any number of characters contained in a string Figure 24-11 –startIndex The index of the first character you want to access in the string –count argument Specifies the number of characters you want to access 17

18 Clearly Visual Basic: Programming with Visual Basic 200818

19 Clearly Visual Basic: Programming with Visual Basic 2008 The Rearrange Name Application User interface –Provides a text box for entering a person’s first name followed by a space and the person’s last name Application –Rearranges name so that the last name comes first, followed by a comma, a space, and the first name Figure 24-12 –Shows code to display rearranged name 19

20 Clearly Visual Basic: Programming with Visual Basic 200820

21 Clearly Visual Basic: Programming with Visual Basic 2008 I Like This Operator Like operator –Allows you to use pattern-matching characters to determine whether one string is equal to another Figure 24-14 –In the syntax, both string and pattern must be String expressions However, pattern can contain one or more of the pattern-matching characters listed in the figure 21

22 Clearly Visual Basic: Programming with Visual Basic 200822

23 Clearly Visual Basic: Programming with Visual Basic 200823

24 Clearly Visual Basic: Programming with Visual Basic 2008 Modifying the Product ID Application Ensure that the five characters in the product ID are three letters followed by two numbers 24

25 Clearly Visual Basic: Programming with Visual Basic 2008 Summary String’s Length property –Stores an integer that represents the number of characters contained in the string Visual Basic –Provides methods that allow you to manipulate strings First character in a string has an index of 0 25


Download ppt "Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section."

Similar presentations


Ads by Google