How to: search within a string (Visual Basic)
This article shows an example of how to search within a string in Visual Basic.
Dim SearchWithinThis As String = "ABCDEFGHIJKLMNOP" Dim SearchForThis As String = "DEF" Dim FirstCharacter As Integer = SearchWithinThis.IndexOf(SearchForThis)
The IndexOf method returns the location of the first character of the first occurrence of the substring. The index is 0-based, which means the first character of a string has an index of 0.
If IndexOf does not find the substring, it returns -1.
The IndexOf method is case-sensitive and uses the current culture.
For optimal error control, you might want to enclose the string search in the
Try block of a Try...Catch...Finally Statement construction.