devxlogo

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

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist