SQL PHP HTML ASP JavaScript articles and free scripts to download

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"
myArray(1)="USA"
myArray(2)="Canada"
myArray(3)="UAE"

For Each item In myArray
Response.Write(item & "<br>")
Next
search_string="UA"
my_filter=Filter(myArray,search_string)


Response.Write "<br>-----After Filter------<br>"
For Each item In my_filter
Response.Write(item & "<br>")
Next

The output of the above code is here

UK
USA
Canada
UAE
-----After Filter------
UAE

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

my_filter=Filter(myArray,search_string,True,1)

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()
Google+

Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer