340 likes | 438 Views
Microsoft Visual Basic 2005: Reloaded Second Edition. Chapter 6 String Manipulation and More Controls. Objectives. After studying this chapter, you should be able to: Determine the number of characters contained in a string Remove characters from a string
E N D
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 6 String Manipulation and More Controls
Objectives After studying this chapter, you should be able to: • 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 • Search a string for one or more characters Microsoft Visual Basic 2005: Reloaded, Second Edition
Objectives (continued) • Access characters contained in a string • Compare strings • Include radio buttons in an interface • Include check boxes in an interface Microsoft Visual Basic 2005: Reloaded, Second Edition
Manipulating Strings in Visual Basic • Most applications need to manipulate string data in some fashion • String properties and methods are used to manipulate string data Microsoft Visual Basic 2005: Reloaded, Second Edition
Determining the Number of Characters Contained in a String Microsoft Visual Basic 2005: Reloaded, Second Edition
Determining the Number of Characters Contained in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Determining the Number of Characters Contained in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Determining the Number of Characters Contained in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Removing Characters from a String • TrimStart method: removes one or more characters from the beginning of a string • TrimEnd method: removes one or more characters from the end of a string • Trim method: removes one or more characters from both the beginning and end of a string • Each of these methods returns a string with the appropriate characters removed Microsoft Visual Basic 2005: Reloaded, Second Edition
Removing Characters from a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Removing Characters from a String (continued) • trimChars argument: • Comma-separated list of characters to be removed • If omitted, spaces will be removed • Default value is the space character: “ ” Microsoft Visual Basic 2005: Reloaded, Second Edition
Removing Characters from a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Removing Characters from a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
The Remove Method • Remove method: • Removes characters from a string • Can remove one or more characters located anywhere in the string • Returns a string with the appropriate characters removed • Index: • The position of a character in a string • Is zero-relative (starts with 0 as first position) Microsoft Visual Basic 2005: Reloaded, Second Edition
The Remove Method (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
The Remove Method (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
The Remove Method (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
The Remove Method (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Replacing Characters in a String • Replace method: replaces a sequence of characters in a string with another sequence of characters Microsoft Visual Basic 2005: Reloaded, Second Edition
Replacing Characters in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Replacing Characters in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Replacing Characters in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Replacing Characters in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
The Mid Statement • Mid statement: • Replaces a specified number of characters in a string with characters from another string • targetString argument: the string in which characters are to be replaced • replacementString argument: the replacement characters • start argument: the starting position for the replacement • count argument: number of characters to replace Microsoft Visual Basic 2005: Reloaded, Second Edition
The Mid Statement (continued) • Character position of characters in the string starts with 1 (not the same as index) • Count argument is optional Microsoft Visual Basic 2005: Reloaded, Second Edition
The Mid Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
The Mid Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
The Mid Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Inserting Characters in a String • PadLeft method: inserts characters at the beginning of a string • PadRight method: inserts characters at the end of a string • length argument: represents the total length of the desired resulting string • character argument: • The character used to pad the string • Default value is the space character Microsoft Visual Basic 2005: Reloaded, Second Edition
Inserting Characters in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Inserting Characters in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Inserting Characters in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Inserting Characters in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition
Inserting Characters in a String (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition