post your comment   print   send to a friend
Rate:   0% | Views: 684
Question categories:  ASP

How can I quickly determine if a string exists within an array?

One way to determine if an array contains an element is to use a simple 'For ... Next' loop to iterate through each element of the array, checking to see if the element is equal to a particular string. For example:

Dim iLoop, bolFound
bolFound = Fals

For iLoop = LBound(SomeArray) to UBound(SomeArray)
If CStr(SomeArray(iLoop)) = CStr(SomeString) then
bolFound = True
End If
Next

When this code completes, bolFound will be True if SomeString is found in SomeArray, and False if not found. This code performs a case-sensitive comparison, however. So, if you are searching for "FooBar" and the array contains an element equal to "Foobar," the bolFound will be False.

We can create a code to determine the position of a particular string element in an array by using the PositionInArray function:

Function ElementInArray(aMyArray, strLookingFor, compare)
'Returns True if strLookingFor is in aMyArray, False otherwise
ElementInArray = (PositionInArray(aMyArray, strLookingFor, compare) > -1)
End Functio

We can also use the Filter function to accomplish a similar task.

Function ElementInArray(aMyArray, strLookingFor, compare)
'Returns True if strLookingFor is in aMyArray, False otherwise
ElementInArray = (UBound(Filter(aMyArray, strLookingFor, True, compare)) > -1)
End Functio

These three techniques will help you to quickly determine if a particular string exists in an array.

Customer Feedback
Rate:   0% | Views: 684 | Please Rate:  
 
If you have other comments or ideas for future technical tips, please type them here:

Email: (optional)

Comments: (optional)

 Business Web Hosting | Php Web Hosting    Back to serch results
Browse the Base
Knowledge Base
Web Design
  Do It Yourself
    ASP
Messages
Private Area
 
Ask
in Private
   
Personal
Folder
 
Related Questions
 
1. How can I display all of the contents of a single-dimension array?
 
2. Does ASP.Net still recognize the global.asa file?
 
3. Is it possible to run client-side .NET code within a browser?
 
4. What happened to date() and time()?
 
5. How do I display data on a web page using arrays instead of Do...While...MoveNext...???
 
Home Browse Search Ask in Private Personal Folder   Help
powered by web hosting 
  Logged as: Guest