250 likes | 268 Views
Clearly Visual Basic: Programming with Visual Basic 2008. Chapter 24 The String Section. 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
E N D
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section
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 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 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 Clearly Visual Basic: Programming with Visual Basic 2008
Get Rid of Those Spaces • Trim method • Usedto 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 Clearly Visual Basic: Programming with Visual Basic 2008
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 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 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 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 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 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 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 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 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 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 Clearly Visual Basic: Programming with Visual Basic 2008