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

devx-admin

devx-admin

Share the Post:
Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1

Copilot Revolution

Microsoft Copilot: A Suit of AI Features

Microsoft’s latest offering, Microsoft Copilot, aims to revolutionize the way we interact with technology. By integrating various AI capabilities, this all-in-one tool provides users with an improved experience that not

AI Girlfriend Craze

AI Girlfriend Craze Threatens Relationships

The surge in virtual AI girlfriends’ popularity is playing a role in the escalating issue of loneliness among young males, and this could have serious repercussions for America’s future. A

AIOps Innovations

Senser is Changing AIOps

Senser, an AIOps platform based in Tel Aviv, has introduced its groundbreaking AI-powered observability solution to support developers and operations teams in promptly pinpointing the root causes of service disruptions