String Manipulation Reference Lecturer Reham O AlAbdul Jabba
String Manipulation Reference: Lecturer Reham O. Al-Abdul Jabba lectures for cap 211
Strings have their own properties and methods, just like a textbox or label or form does.
Length To get the length of a string: Text. Length = First. Name. Length
Chars To get the character at a specified position. Dim One. Character As Char For i = 0 To Text. Length - 1 One. Character = First. Name. Chars(i) Msg. Box(One. Character) Next
To. Upper, To. Lower Dim str. Upper As String str. Upper = Text. Box 1. Text. Box 2. Text = str. Upper. To. Upper Text. Box 3. Text = str. Upper. To. Lower • Notice that the name of the variable you want to do something with comes first. Then, after the full stop, you add the name of the method.
Trim One of the methods on our list is Trim. What this does is to trim any leading or trailing blank spaces from a string. So if the string was “ Text ”, then Trim would delete those spaces for you, leaving just “Text”. You use it in your code like this: First. Name = txt. First. Text First. Name = First. Name. Trim
In. Str( ) The In. Str( ) method of string variables tells you what the position of one string is inside another. For example, if your string was “me@me. com” and you wanted to know if the string contained the @ symbol, you could use In. Str( ) Method. You would use it like this: First. String = “me@me. com” Second. String = “@” position = In. Str(First. String, Second. String)
In. Str( ) The variable First. String is the string we want to search; Second. String is what we want to search for. You can specify a starting position for the search to begin. If you do, this number goes at the start (the default is zero): position = In. Str(1, First. String, Second. String) In the code above, position would have a value of 3. That’s because the @ symbols starts at the third letter of “me@me. com”.
In. Str( ) Note: the In. Str() Method starts counting at 1, and not zero like Chars(), which is very confusing! If the string you’re searching for is not found, then the value placed inside of your integer variable (position in our case) is zero. That enables you to code something like this: If position = 0 Then Msg. Box (“Not a Valid email address: There was No @ Sign”) End If
Substring This allows you to grab one string within another. (For example, if you wanted to grab the “. com” from the email address “me@me. com. ”) In between the round brackets of Substring( ), you specify a starting position and then how many characters you want to grab (the count starts at zero again). Like this: Dim Email as String Dim Dot. Com as String Email = “me@me. com” Dot. Com = Email. Substring(5, 4) Msg. Box(Dot. Com)
Substring You could also do a check to see if the email address ended in “. com” like this. Here’s some code to do the job: Dim Email As String Dim Dot. Com As String Email = "me@me. com" Dot. Com = Email. Substring(Email. Length - 4, 4) If Dot. Com = ". com" Then Msg. Box("Ends in Dot Com") Else Msg. Box("Doesn't End in Dot Com") End If
Substring method could do the same function as Chars() method and the result would be the same. Like this: For i = 0 To Text. Length - 1 One. Character = First. Name. Substring(i, 1) Msg. Box One. Character Next
Substring Dim s As String = "Welcome to the world" s = s. Substring(8) ‘ “to the world ” Dim r As String = "Welcome to the world" r = r. Sub. String(8, 6) ‘ “to the”
Replace You can replace text in one string with some other text. Dim Old. Text As String Dim New. Text As String Old. Text = "This is some test" New. Text = Old. Text. Replace("test", "text") Msg. Box(Old. Text) Msg. Box(New. Text) When you run the program, the first message box will say "This is some test" and the second box will say "This is some text".
Insert You can also insert some new text into an string. Dim Some. Text As String Dim New. Text As String Some. Text = "This some text" New. Text = Some. Text. Insert(5, "is ") Msg. Box(Some. Text) Msg. Box(New. Text) The 5 in round brackets means start at position 5 in the string variable Some. Text (the count starts at zero). You then type the text that you want inserted. The first message box This some text The second message box This is some text
Split and Join Split allows you to split a line of text and put each element (word or phrase) into an array; Join allows you to join elements of an array into one line of text.
Split You can read the text file line by line, and each line might be something like this: “User. Name 1, Password 1, User. Name 2, Password 2, User. Name 3, Password 3” The programming problem is to separate each word. You can use Split for this. Each word would then be separated, ready for you to place into an array.
Split Dim Line. Of. Text As String Dim i As Integer Dim ary. Text. File() As String Line. Of. Text = "User. Name 1, Password 1, User. Name 2, Password 2" ary. Text. File = Line. Of. Text. Split(", ") For i = 0 To UBound(ary. Text. File) Msg. Box(ary. Text. File(i)) Next I When VB finishes the splitting, it fills up your array. Each element will occupy one slot in your array. So in our example, ary. Text. File(0) will hold a value of User. Name 1, ary. Text. File(1) will hold a value of Password 1, etc.
Join The Join method is used when you want to join the elements of an array back together again. Here’s some code which does exactly that: Dim Line. Of. Text As String Dim i As Integer Dim ary. Text. File(3) As String ary. Text. File(0) = "User. Name 1" ary. Text. File(1) = "Password 1" ary. Text. File(2) = "User. Name 2" ary. Text. File(3) = "Password 2" Line. Of. Text = Line. Of. Text. Join("-", ary. Text. File) Msg. Box(Line. Of. Text) you first type what you want to use as a separator. Here, we’re using an hyphen as a separator. Next, you put the name of your array. The variable Line. Of. Text will hold the following: "User. Name 1 -Password 1 -User. Name 2 -Password 2"
To. Char. Array Dim string 1 As String = "hello" Dim char. Array() As Char = string 1. To. Char. Array() Dim i As Integer Console. Write. Line("string 1 = " & string 1) Console. Write("string 1 as an array of character =") For i = 0 To char. Array. Length - 1 Console. Write(" " & char. Array(i)) Next string 1 = hello string 1 as an array of character = h e l l o
Equals In code previously, we had this: If Dot. Com = ". com" Then Msg. Box("Ends in Dot Com") Else Msg. Box("Doesn't End in Dot Com") End If You can use the Equals method of string variables in the first line, instead of an equals sign: If Dot. Com. Equals(“. com”) Then
Equals Str 1. Equals(Str 2) variable String. Equals(Str 1, Str 2) namespace Both returns true or false
Compare. To Str 1. compare. To (str 2) Returns: 0 -1 1 if equal if str 1 < str 2 …. ASCII codes if str 1 > str 2
24 Dim a As String = "Ahmad" Dim b As String = "Ali" If (a. Compare. To(b) = 0) Then Label 1. Text = "equals" End If If (a. Compare. To(b) = -1) Then Label 1. Text = "ahmad <ali" End If If (a. Compare. To(b) = 1) Then Label 1. Text = "ahmad>ali" 2/18/2021 End If
Str. Reverse If you wish to flip around the front and back end of a string, then the Str. Reverse(string) is for you. It is used in the following way. This would pop up a message saying ‘looc si 112 pac’ Msg. Box(Str. Reverse(“cap 211 is cool"))
String Methods Chr (65) A Asc (“Apple”) 65 Asc(“Ahmad”) 65 “ 32” & chr(176) & “Fahrenheit” 32°Fahrenheit
Char Methods • Test characters to determine whether they are of specific character type and that perform case conversions on characters. Is. Digit Is. Letter. Or. Digit Is. Lower Is. Upper To. Lower
Char Methods Dim ch As Char = Console. Read. Line If Char. Is. Digit( ch ) then Console. Write. Line(“Digit”) Else Console. Write. Line(“Not Digit”) End If
String Properties and Methods "Visual". Length is 6. "Visual". To. Upper is VISUAL. "123 Hike". Length is 8. "123 Hike". To. Lower is 123 hike. "a" & " bcd ". Trim & "efg" is abcdefg. 29
Positions in a String Positions of characters in a string are numbered 0, 1, 2, …. Consider the string “Visual Basic”. Position 0: V Position 1: i Position 7: B Substring “al” begins at position 4 30
Substring Method Let str be a string. str. Substring(m, n) is the substring of length n, beginning at position m in str. “Visual Basic”. Substring(2, 3) is “sua” “Visual Basic”. Substring(0, 1) is “V” 31
Index. Of Method Let str 1 and str 2 be strings. str 1. Index. Of(str 2) is the position of the first occurrence of str 2 in str 1. (Note: Has value -1 if str 2 is not a substring of str 1. ) "Visual Basic". Index. Of("is") is 1. "Visual Basic". Index. Of("si") is 9. "Visual Basic". Index. Of("ab") is -1. 32
The Empty String The string "", which has no characters, is called the empty string or the zero-length string. The statement lst. Box. Items. Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement txt. Box. Clear() or the statement txt. Box. Text = "" 33
Initial Value of a String Variable By default the initial value is the keyword Nothing Strings can be given a different initial value as follows: Dim name As String = "Fred" 34
More on Strings For more information on String methods check Deitel’s book It is helpful for validating inputs, you may need it when coding your project. You must practice lots of string manipulation examples check the following websites: http: //msdn. microsoft. com/enus/library/aa 903372(VS. 71). aspx http: //www. a 1 vbcode. com/code. asp? type=s trings
- Slides: 35