ArrayFilter – Filtering arrays of any type, with many options

ArrayFilter – Filtering arrays of any type, with many options

' Filter the input array. It can work with string arrays or arrays of other ' type!' The input array is modified by removing the elements that do not match the ' filter options, but the array is not actually re-sized. The filter results ' will be moved at the beginning of the array, and empty elements will be left ' at the end. The function returns the number of elements that match the filter ' options however, so that you can manually redim the array if you want to.' ' Example:'   Dim arr() As String = {"this is a String", "this is another one",'  "test string", "will it work?", "ok"}'   ' include all the elements that have "string" within them'   Dim numElements As Integer = ArrayFilter(arr, "string", False, False, True)''   Dim i As Integer'   Debug.WriteLine("--- Start ---")'   For i = 0 To numElements - 1'       Debug.WriteLine(arr(i))'   Next'   Debug.WriteLine("--- End ---")''   ' exclude all the 4 elements'   Dim arr2() As Integer = {1, 4, 7, 9, 11, 43, 56, 4, 11, 1}'   numElements = ArrayFilter(arr2, 4, False, False, False)''   Debug.WriteLine("--- Start ---")'   For i = 0 To numElements - 1'       Debug.WriteLine(arr2(i))'   Next'   Debug.WriteLine("--- End ---")Function ArrayFilter(ByVal arr As Array, ByVal search As Object) As Integer    Return ArrayFilter(arr, search, True)End Function' This overloaded version adds the possibility to choose whether the search is ' case sensitive or notFunction ArrayFilter(ByVal arr As Array, ByVal search As Object, _    ByVal caseSensitive As Boolean) As Integer    Return ArrayFilter(arr, search, caseSensitive, True)End Function' This overloaded version adds the possibility to choose whether partial search ' results are accepted or notFunction ArrayFilter(ByVal arr As Array, ByVal search As Object, _    ByVal caseSensitive As Boolean, ByVal exactMatch As Boolean) As Integer    Return ArrayFilter(arr, search, caseSensitive, exactMatch, True)End Function' This overloaded version adds the possibility to choose whether the search ' results are included or excluded in/from the filtered arrayFunction ArrayFilter(ByVal arr As Array, ByVal search As Object, _    ByVal caseSensitive As Boolean, ByVal exactMatch As Boolean, _    ByVal include As Boolean) As Integer    Return ArrayFilter(arr, search, caseSensitive, exactMatch, include, _        arr.GetLowerBound(0), arr.GetUpperBound(0))End Function' This overloaded version adds the possibility to choose the portion of the ' array to be filteredFunction ArrayFilter(ByVal arr As Array, ByVal search As Object, _    ByVal caseSensitive As Boolean, ByVal exactMatch As Boolean, _    ByVal include As Boolean, ByVal first As Integer, ByVal last As Integer) As Integer    Dim strSearch As String    Dim removeElement As Boolean    Dim isStringArr As Boolean    Dim numRemoved As Integer    ' if this is a String array,    If TypeOf arr Is String() Then        isStringArr = True        ' convert the search object to a string, and if the caseSensitive         ' option is not True, convert it to lower case        strSearch = search.ToString()        If Not caseSensitive Then strSearch = strSearch.ToLower()    End If    Dim i As Integer    For i = last To first Step -1        removeElement = False        ' if this is a string array...        If isStringArr Then            ' ...convert the array's item to string, and to lower case is the             ' filter is not case sensitive            Dim strValue As String = CType(arr.GetValue(i), String)            If Not caseSensitive Then strValue = strValue.ToLower()            If exactMatch Then                ' if this is an exact match, remove or leave the element                 ' according to the include option                If strSearch = strValue Then                    If Not include Then removeElement = True                Else                    If include Then removeElement = True                End If            Else                ' if this is a partial match, remove or leave the element                 ' according to the include option                If strValue.IndexOf(strSearch) > -1 Then                    If Not include Then removeElement = True                Else                    If include Then removeElement = True                End If            End If        Else            ' if not a string array, use the Object's Equals method for value             ' comparison            Dim oValue = arr.GetValue(i)            If oValue.Equals(search) Then                If Not include Then removeElement = True            Else                If include Then removeElement = True            End If        End If        If removeElement Then            ' shift elements from arr(index+1) to arr(index)            Array.Copy(arr, i + 1, arr, i, arr.GetUpperBound(0) - i)            ' clear the last element            arr.Clear(arr, arr.GetUpperBound(0), 1)            ' increment the counter of removed elements            numRemoved += 1        End If    Next    Return arr.Length - numRemovedEnd Function

Share the Post:
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

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as