Filter() searches a one-dimensional array of strings and returns a zero-based array containing values that include or exclude the requested substring.
Filter(sourceArray, match, include, compare)
include is optional: True returns matching elements and False returns non-matching elements. Use vbBinaryCompare for case-sensitive binary comparison or vbTextCompare for text comparison.
Dim myArray(3), item, searchString, filtered
myArray(0) = "UK"
myArray(1) = "USA"
myArray(2) = "Canada"
myArray(3) = "UAE"
For Each item In myArray
Response.Write item & "<br>"
Next
searchString = "UA"
filtered = Filter(myArray, searchString)
Response.Write "<br>-----After Filter------<br>"
For Each item In filtered
Response.Write item & "<br>"
Next
UK
USA
Canada
UAE
-----After Filter------
UAE
filtered = Filter(myArray, searchString, True, vbTextCompare)
Set include to False when you want the values that do not contain the match. Filter() performs substring matching; for searching within one string, use InStr().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.