Objective 7 04 Apply Builtin String Functions 3
Objective 7. 04 Apply Built-in String Functions (3%) COMPUTER PROGRAMMING I
Objective/Essential Standard �Essential Standard 7. 00 Apply Advanced Logic �Indicator 7. 04 Apply Built-in String Functions (3%)
The String Class �The String data type is a class. �A class includes properties and methods called members. �When a class is used to create a variable, the variable is called an object. �An object accesses a member of its class with a dot (. ) between the object name and the member name.
Strings �Remember with strings the first letter is at index position 0. �The last letter is always at the length of the string -1. �The middle letter is always at the length of the string /2 School 012345
String Class Properties �You can get the length of any string by using the Length property. int. Length = str. Name. Length No ( )’s �You can get any character that is part of the string by using the Chars property. chr. Letter = str. Name. Chars(0) This must be an integer or integer variable that represents the index position.
String Class Functions �Visual Studio provides the programmer with multiple built-in functions from the String class, including the following: String Class Functions � Compare() � Remove() � Concat() � Replace() � Equals() � To. Lower() � Format() � To. Upper() � Index. Of() � To. String() � Insert() � Trim(), Trim. End(), Trim. Start() �The String class is not limited to these functions.
String Class Function Purpose Compare() Compares two specified String objects and returns an integer that indicates their relative position in the sort order Concat() Creates the string representation of a specified object, usually two or more strings Equals() Determines whether this instance and another specified String object have the same value Index. Of() Reports the index of the first occurrence of the specified Unicode character (or string) in this string Insert() Inserts a specified instance of String at a specified index position in this instance Remove() Deletes all the characters from this string beginning at a specified position and continuing through the last position
String Class Function Purpose Replace() Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string To. Lower() Returns a copy of this string converted to lowercase To. Upper() Returns a copy of this string converted to uppercase Trim() Removes all leading and trailing white-space characters from the current String object Trim. End() Removes all trailing occurrences of a set of characters specified in an array from the current String object Trim. Start() Removes all leading occurrences of a set of characters specified in an array from the current String object
The Compare() Function �The Compare() function has several options. We will look at the most basic one. This method is typically used in an If statement. �The comparison looks at the lexical relationship of the two strings. String. Compare(str. One, str. Two) int. Num Value after execution <0 str. One is less than str. Two =0 str. One equals str. Two >0 str. One is greater than str. Two
The Compare() Function � Let’s look at this table that tells us what the decimal equivalents of our characters are.
The Compare() Function �The Compare() function looks at these values. �Example: str. First = Apple str. Last = Banana A 65 B 66 �Code String. Compare (str. First, str. Last) �Result 65 – 66 = -1 so String. Compare (str. First, str. Last) < 0 therefore Apple “comes before” Banana
The Compare() Function Dim str. A As String = "Apple" Dim str. B As String = "Banana" If String. Compare(str. A, str. B) = 0 Then lbl. Answer. Text = str. A & " is equal to " & str. B Else. If String. Compare(str. A, str. B) < 0 Then lbl. Answer. Text = str. A & " comes before " & str. B Else. If String. Compare(str. A, str. B) > 0 Then lbl. Answer. Text = str. A & " comes after " & str. B End If
The Concat() Function �The concat() function will concatenate (merge) strings. Dim str. A As String = "Apple" Dim str. B As String = "Banana" Dim str. New As String str. New = String. Concat(str. A, str. B) lbl. Answer. Text = str. New ‘str. New = Apple. Banana
The Equals() Function �The Equals function returns a Boolean value (true or false) after comparing the values of two strings. Dim str. A As String = "Apple" Dim str. B As String = "Banana" If str. A. Equals(str. B) Then lbl. Answer. Text = "The strings are the same. " Else lbl. Answer. Text = "The strings are the different. " End If
The Index. Of() Function �The Index. Of() function has several different variations that will return the index position of a character or string. Dim str. A As String = "Apple" Dim int. Index As Integer int. Index = str. A. Index. Of("p") lbl. Answer. Text = int. Index ‘int. Index = 1
The Insert() Function �The Insert() function inserts a string at a specified index position. Dim str. A As String = "Apple" Dim str. New As String str. New = str. A. Insert(5, "s") lbl. Answer. Text = str. New ‘str. New = Apples
The Remove() Function � The Remove() function returns a new string where the specified string has been deleted. � Remove() has two options that will delete all of a string or a specified number of characters. � Remove(int. Start. Index) int. Start represents the index position to start removing characters This option will remove all letters from the starting index position to the end. � Remove(int. Start. Index, int. Num. Chars. To. Remove) int. Start represents the index position to start removing characters This option will remove all letters starting at the start index position and removing the given number of characters.
The Remove() Function Dim str. A As String = "Apple" Dim str. B As String = "Banana" Dim str. New, str. New 2 As String str. New = str. A. Remove(0) str. New 2 = str. B. Remove(0, 1) lbl. Answer. Text = str. New 2 & " " & str. New ‘displays anana as Apple is deleted completely
The Replace() Function �The Replace() function returns a new string that has the specified string or character replaced in all occurences. Dim str. A As String = "Apple" Dim str. B As String = "Banana" Dim str. New, str. New 2 As String str. New = str. A. Replace("p", "b") str. New 2 = str. B. Replace("n", "") lbl. Answer. Text = str. New & " ‘ displays abble Baaa " & str. New 2
The To. Lower() Function �The To. Lower() function returns a copy of the string in lowercase. Dim str. A As String = "Apple“ lbl. Answer. Text = str. A. To. Lower ‘Displays apple
The To. Upper() Function �The To. Upper() function returns a copy of the string in uppercase. Dim str. A As String = "Apple“ lbl. Answer. Text = str. A. To. Upper ‘Displays APPLE
The Trim() Function �The Trim() function removes all leading and trailing blanks from the string. Dim str. A As String = " Dim str. Ex As String = "Example: " Dim str. New As String str. New = str. A. Trim lbl. Answer. Text = str. Ex & str. New ‘Displays Example: Apple "
The Trim. End() Function �The Trim. End() function deletes all blanks from the end of the string. Dim str. A As String = " Apple Dim str. Ex As String = "Example: " Dim str. New As String str. New = str. A. Trim. End lbl. Answer. Text = str. Ex & str. New ‘Displays Example: Apple "
The Trim. Start() Function �The Trim. Start() function deletes all blanks from the beginning of the string. Dim str. A As String = " Apple Dim str. Ex As String = "Example: " Dim str. New As String str. New = str. A. Trim. Start lbl. Answer. Text = str. Ex & str. New ‘Displays Example: Apple "
Try It! �Create a new application called string. Example Save it into the location as instructed by your teacher. �Add the following controls. �When the button is clicked, the appropriate answer should be displayed in the lbl. Answer label. �For the following functions you will only use the first textbox when clicking the button to display the answer To. Lower, To. Upper Trim, Trim. End, Trim. Start �For functions such as Replace and Insert, use the first textbox as “Word” input and the second textbox as the input to delete/replace/remove, etc.
Try It! Control Name Text/Items Label lbl. Prompt 1 Enter a Word: Label lbl. Prompt 2 Enter a Word: Button btn. Compare Button btn. Concat Button btn. Equals Button btn. Insert Button btn. Remove Button btn. Replace Button btn. To. Lower Button btn. To. Upper Button btn. Trim. End Button btn. Trim. Start
Try It! Solution – Visual Basic Private Sub btn. Compare_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Compare. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text If String. Compare(str. A, str. B) = 0 Then lbl. Answer. Text = str. A & " is equal to " & str. B Else. If String. Compare(str. A, str. B) < 0 Then lbl. Answer. Text = str. A & " comes before " & str. B Else. If String. Compare(str. A, str. B) > 0 Then lbl. Answer. Text = str. A & " comes after " & str. B End If End Sub Private Sub btn. Concat_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Concat. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text Dim str. New As String str. New = String. Concat(str. A, str. B) lbl. Answer. Text = str. New End Sub
Try It! Solution Private Sub btn. Equals_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Equals. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text If str. A. Equals(str. B) Then lbl. Answer. Text = "The strings are the same. " Else lbl. Answer. Text = "The strings are the different. " End If End Sub Private Sub btn. Replace_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Replace. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text Dim str. New, str. New 2 As String str. New = str. A. Replace(str. B, "b") lbl. Answer. Text = str. New & " " & str. New 2 End Sub
Try It! Solution Private Sub btn. Index. Of_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Index. Of. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text Dim int. Index As Integer int. Index = str. A. Index. Of(str. B) lbl. Answer. Text = int. Index End Sub Private Sub btn. Insert_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Insert. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text Dim str. New As String str. New = str. A. Insert(5, "s") lbl. Answer. Text = str. New End Sub Private Sub btn. Remove_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Remove. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text Dim str. New, str. New 2 As String str. New = str. A. Remove(0) str. New 2 = str. B. Remove(0, 1) lbl. Answer. Text = str. New 2 & " " & str. New End Sub
Try It! Solution Private Sub btn. To. Lower_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. To. Lower. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text lbl. Answer. Text = str. A. To. Lower End Sub Private Sub btn. To. Upper_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. To. Upper. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text lbl. Answer. Text = str. A. To. Upper End Sub Private Sub btn. Trim_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Trim. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text Dim str. Ex As String = "Example: " Dim str. New As String str. New = str. A. Trim lbl. Answer. Text = str. Ex & str. New End Sub
Try It! Solution Private Sub btn. Trim. End_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Trim. End. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text Dim str. Ex As String = "Example: " Dim str. New As String str. New = str. A. Trim. End lbl. Answer. Text = str. Ex & str. New End Sub Private Sub btn. Trim. Start_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Trim. Start. Click Dim str. A As String = Me. txt. Word 1. Text Dim str. B As String = Me. txt. Word 2. Text Dim str. Ex As String = "Example: " Dim str. New As String str. New = str. A. Trim. Start lbl. Answer. Text = str. Ex & str. New End Sub
Conclusion �This Power. Point provided an overview of several methods in the String class. �For more information on this topic http: //msdn. microsoft. com/enus/library/system. string_methods. aspx
- Slides: 32