String Manipulation and More Controls
Objectives 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 Access characters contained in a string Compare strings Include radio buttons in an interface Include check boxes in an interface 1/3/2019 VB 2005
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 1/3/2019 VB 2005
Determining the Number of Characters Contained in a String Length Property Syntax String.length Example NumChar=ziptextbox.text.length 1/3/2019 VB 2005
Removing Characters from a String TrimStart method: removes one or more characters from the beginning of a string Syntax: String.TrimStart([trimChars]) TrimEnd method: removes one or more characters from the end of a string String.TrimEnd([trimChars]) 1/3/2019 VB 2005
Removing Characters from a String (continued) Trim method: removes one or more characters from both the beginning and end of a string Syntax String.TrimEnd([trimChars]) Each of these methods returns a string with the appropriate characters removed 1/3/2019 VB 2005
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: “ ” 1/3/2019 VB 2005
The Remove Method Index: 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 Syntax: string.rRemove(startIndex, count) Index: The position of a character in a string Is zero-relative (starts with 0 as first position) 1/3/2019 VB 2005
Replacing Characters in a String Replace method: replaces a sequence of characters in a string with another sequence of characters Syntax: String.Replace (oldValue, newValue) 1/3/2019 VB 2005
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 1/3/2019 VB 2005
The Mid Statement (continued) Character position of characters in the string starts with 1 (not the same as index) Count argument is optional Syntax: Mid(targetString,start [,count])=ReplacementString 1/3/2019 VB 2005
Inserting Characters in a String PadLeft method: inserts characters at the beginning of a string Syntax String.PadLeft(Length[, Character]) PadRight method: inserts characters at the end of a string String.PadlRight(Length[, Character]) 1/3/2019 VB 2005
Inserting Characters in a String (continued) 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 1/3/2019 VB 2005