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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.