Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition.

Similar presentations


Presentation on theme: "Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition."— Presentation transcript:

1 Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition

2 Objectives Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 2 After studying Chapter 23, you should be able to: 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

3 Objectives (cont’d.) Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 3 Access characters in a string Remove characters located anywhere in a string Compare strings using pattern-matching

4 Working with Strings Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 4 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

5 How Many Characters Are There? Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 5 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

6 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 6 Figure 23-1 Syntax and examples of the Length property

7 Get Rid of Those Spaces Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 7 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

8 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 8 Figure 23-2 Syntax and examples of the Trim method

9 The Product ID Application Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 9 Displays (in a label control): A listing of the product IDs entered by the user Each product ID must contain exactly five characters

10 Let’s Make a Substitution Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 10 Replace method Used to replace a sequence of characters in a string with another sequence of characters Figure 23-4 oldValue The sequence of characters that you want to replace in the string newValue The replacement characters

11 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 11 Figure 23-4 Syntax and examples of the Replace method

12 I Need To Fit This in Somewhere Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 12 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

13 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 13 Figure 23-5 Syntax and examples of the Insert method

14 The Phone Numbers Application Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 14 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 23-7 and 23-9 Show code and resulting display

15 The Phone Numbers Application (cont’d.) Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 15 Figure 23-7 Statements needed to insert the two hyphens

16 The Phone Numbers Application (cont’d.) Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 16 Figure 23-9 Statements needed to insert the two hyphens

17 Where Does It Begin? Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 17 IndexOf method Used to search a string to determine whether it contains a specific sequence of characters Examples Determining whether the area code “(312)” appears in a phone number Determining whether “Elm Street” appears in an address

18 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 18 Figure 23-10 Syntax and examples of the IndexOf method

19 I Just Want a Part of It Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 19 Substring method Used to access any number of characters contained in a string Figure 23-11 startIndex The index of the first character you want to access in the string numCharsToAccess argument Specifies the number of characters you want to access

20 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 20 Figure 23-11 Syntax and examples of the Substring method

21 The Rearrange Name Application Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 21 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 23-12 Shows code to display rearranged name

22 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 22 Figure 23-12 btnRearrange control’s Click event procedure

23 Throw Away Those Characters Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 23 Remove method Removes specified number of characters Characters may be located anywhere in a string startIndex argument Index of first character you want removed from the string numCharsToRemove argument Specifies number of characters you want removed

24 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 24 Figure 23-14 Syntax and examples of the Remove method

25 The Last Name Application Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 25 Interface provides a text box User enters a person’s first name, a space, and the last name Application displays only the person’s last name

26 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 26 Figure 23-15 btnDisplay control’s Click event procedure

27 I Like This Operator Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition 27 Like operator Allows you to use pattern-matching characters to determine whether one string is equal to another Figure 23-17 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

28 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 28 Figure 23-17 Syntax and examples of the Like operator (continues)

29 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 29 Figure 23-17 Syntax and examples of the Like operator (continues)

30 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 30 Figure 23-17 Syntax and examples of the Like operator (cont’d.)

31 Modifying the Product ID Application Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 31 Ensure that the five characters in the product ID are three letters followed by two numbers

32 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 32 Figure 23-18 Click event procedure for the btnAdd control

33 Summary Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 33 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


Download ppt "Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition."

Similar presentations


Ads by Google