Searching inside an array for matching elements using Filter function.

We can search inside an array for a string using Filter() built in function of VB used in ASP. The search or find operation will return another array with the result. We can use different arguments to match our requirement while using Filter function. This function works like different types of string functions like InStr, Replace etc. Here is the syntax of Filter() function.

Filter(my_array, search_string, include, comparetype)
Here my_array is our main array inside which we will be searching, search_string is the string variable or the string itself to be searched inside my_array. The other two arguments include and comparetype are optional arguments. include is a Boolean value, it can be True or False. If it is True then all matching elements of the my_array is returned and if it is set to False then all not matching elements are returned. The argument comparetype can take value of 0 or 1, if it is set to 0 then it is a binary comparison so it is case sensitive as a result lower and upper case letters are treated as different. If the value of comparetype is set to 1 then it will be a text comparison so it is case insensitive, as a result lower case and upper case text are treated as same.

Let us try with this simple code on how to use Filter function.

  Dim myArray(3) 'Declaring a dynamic array
myArray(0) = "UK"

For Each item In myArray
Response.Write(item & "<br>")

Response.Write "<br>-----After Filter------<br>"
For Each item In my_filter
Response.Write(item & "<br>")
The output of the above code is here

-----After Filter------
By default the argument include is set to True and comparetype is set to 0. So if we search for lower case word ua ( in place of UA ), we will get empty result. We can add the value of these two optional arguments and see the result. The Filter function can be modified like this
You will get different results by making include = False and changing comparetype to 0. Similar function in ASP for searching an array is in_array()

Be the first to post comment on this article :


Post Comment This is for short comments only. Use the forum for more discussions.

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 All rights reserved worldwide Privacy Policy Disclaimer