328 Part II Programming Fundamentals
String
Method or
Property
Visual
Basic
Function Description String Example
Contains Instr Determines whether the
specified string occurs in
the current string.
Dim region As String
Dim result As Boolean
region = "Germany"
result = region.Contains("Ge")
'result = True
Substring Mid Returns a fixed number
of characters in a string
from a given starting point.
(Note: The first element in
a string has an index of 0 .)
Dim Cols, Middle As String
Cols = "First Second Third"
Middle = Cols.SubString(6, 6)
'Middle = "Second"
IndexOf InStr Finds the starting point of
one string within a larger
string.
Dim Name As String
Dim Start As Short
Name = "Abraham"
Start = Name.IndexOf("h")
'Start = 4
Trim Trim Removes leading and
following spaces from a
string.
Dim Spacey, Trimmed As String
Spacey = " Hello "
Trimmed = Spacey.Trim
'Trimmed = "Hello"
Remove N/A Removes characters from
the middle of a string.
Dim RawStr, CleanStr As String
RawStr = "Hello333 there"
CleanStr = RawStr.Remove(5, 3)
'CleanStr = "Hello there"
Insert N/A Adds characters to the
middle of a string.
Dim Oldstr, Newstr As String
Oldstr = "Hi Felix"
Newstr = Oldstr.Insert(3, "there
")
'Newstr = "Hi there Felix"
Compare StrComp Compares strings and can
disregard case differences.
Dim str1 As String = "Soccer"
Dim str2 As String = "SOCCER"
Dim Match As Integer
Match = String.Compare(str1, _
str2, True)
'Match = 0 [strings match]
CompareTo StrComp Compares a string to the
current string and checks
for case differences
Dim str1 As String = "Soccer"
Dim str2 As String = "SOCCER"
Dim Match As Integer
Match = str1.CompareTo(str2)
'Match = -1 [strings do not
match]
Replace Replace Replaces all instances of a
substring in a string with
another string.
Dim Oldstr, Newstr As String
Oldstr= "*se*ll"
Newstr = Oldstr.Replace( _
"*", "ba")
'Newstr = "baseball"