There are several overloads of IndexOf
that take a StringComparison
parameter, allowing you to specify the various culture and case-sensitivity options.
For example:
Dim idx As Integer = haystack.IndexOf(needle, StringComparison.OrdinalIgnoreCase)
As for counting occurrences, there's nothing built-in, but it's pretty straightforward to do it yourself:
Dim haystack As String = "The quick brown fox jumps over the lazy dog"
Dim needle As String = "th"
Dim comparison As StringComparison = StringComparison.OrdinalIgnoreCase
Dim count As Integer = CountOccurrences(haystack, needle, comparison)
' ...
Function CountOccurrences(ByVal haystack As String, ByVal needle As String, _
ByVal comparison As StringComparison) As Integer
Dim count As Integer = 0
Dim index As Integer = haystack.IndexOf(needle, comparison)
While index >= 0
count += 1
index = haystack.IndexOf(needle, index + needle.Length, comparison)
End While
Return count
End Function