1 / 25

Clearly Visual Basic: Programming with Visual Basic 2008

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

Download Presentation

Clearly Visual Basic: Programming with Visual Basic 2008

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  5. Clearly Visual Basic: Programming with Visual Basic 2008

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

  7. Clearly Visual Basic: Programming with Visual Basic 2008

  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 Clearly Visual Basic: Programming with Visual Basic 2008

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

  10. Clearly Visual Basic: Programming with Visual Basic 2008

  11. 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

  12. Clearly Visual Basic: Programming with Visual Basic 2008

  13. 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

  14. Clearly Visual Basic: Programming with Visual Basic 2008

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

  16. Clearly Visual Basic: Programming with Visual Basic 2008

  17. 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

  18. Clearly Visual Basic: Programming with Visual Basic 2008

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

  20. Clearly Visual Basic: Programming with Visual Basic 2008

  21. 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

  22. Clearly Visual Basic: Programming with Visual Basic 2008

  23. Clearly Visual Basic: Programming with Visual Basic 2008

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

  25. 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

More Related