ArrayLastIndexOf – An extended version of Array.LastIndexOf

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing