ArrayLastIndexOf – An extended version of Array.LastIndexOf

' An extended version of Array.LastIndexOf, with more options for string ' searches. The function returns the index of the found element,'  or -1 if no element is found'' Example:'   Dim arr As String() = {"this is a string for my test", "test number 2",'  "another String", "do you like this string?", "I like running tests"}'   Dim i As Integer = arr.GetUpperBound(0) + 1''   Do'       i = ArrayLastIndexOf(arr, "string", i - 1, True, False)'       If i > -1 Then'           Debug.WriteLine(arr(i))'       End If'   Loop While i > -1Function ArrayLastIndexOf(ByVal arr As String(), ByVal search As String) As _    Integer    Return ArrayLastIndexOf(arr, search, arr.GetUpperBound(0))End Function' this overloaded version allows you to specify also the array's index to start ' fromFunction ArrayLastIndexOf(ByVal arr As String(), ByVal search As String, _    ByVal startIndex As Integer) As Integer    Return ArrayLastIndexOf(arr, search, startIndex, True)End Function' this overloaded version allows you to specify also whether the search is case-' sensitiveFunction ArrayLastIndexOf(ByVal arr As String(), ByVal search As String, _    ByVal startIndex As Integer, ByVal caseSensitive As Boolean) As Integer    Return ArrayLastIndexOf(arr, search, startIndex, caseSensitive, True)End Function' this overloaded version allows you to specify also whether only exact matches ' are validFunction ArrayLastIndexOf(ByVal arr As String(), ByVal search As String, _    ByVal startIndex As Integer, ByVal caseSensitive As Boolean, _    ByVal exactMatch As Boolean) As Integer    ' if the search is case-sensitive and it runs against exact matches only,    '  use the standard Array.LastIndexOf function    If caseSensitive And exactMatch Then        Return Array.LastIndexOf(arr, search, startIndex)    End If    If Not caseSensitive Then search = search.ToLower()    Dim i As Integer    For i = startIndex To 0 Step -1        Dim currElem As String = arr(i)        ' if the search is not case-sensitive, convert everything to lower-case        If Not caseSensitive Then currElem = currElem.ToLower()        If exactMatch Then            If search = currElem Then Return i        Else            ' if partial matches are ok, use the String.IndexOf function,            '  and return the            ' current index if a partial match is found            Dim j As Integer = currElem.IndexOf(search)            If j > -1 Then Return i        End If    Next    ' if we get here, no element matches the search options, so return -1    Return -1    ' =============================    ' Alternative implementation that clones and reverses the input array,    '  and passes it to ArrayIndexOf    ' It results to be slower that the implementation above though    '    'Dim arrRev As String() = arr.Clone()    'Array.Reverse(arrRev)    'Dim i As Integer = ArrayIndexOf(arrRev, search,    '  arr.GetUpperBound(0) - startIndex, caseSensitive, exactMatch)    'If i = -1 Then    '    Return i    'Else    '    Return arr.GetUpperBound(0) - i    'End If    ' =============================End Function

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: