Microsoft Office 2000/Visual Basic Programmer's Guide   

The Filter Function

The Filter function searches a string array for all elements that match a given text string. The Filter function takes three arguments: a string array, a string containing the text to find, and a constant specifying the string-comparison method. It returns a string array containing all the matches that it finds.

You can use the Filter function to determine whether a particular element exists in an array. An example, the ConvertToProperCase procedure, appears in "Converting Strings" later in this chapter.

When working with the Filter function, you may notice that it returns a particular element even if only part of the element matches the search text. In other words, if your search text is the letter "e," and the array you're searching contains the element "test," the array returned by the Filter function will contain the element "test."

Given this behavior, you might be tempted to use the Filter function to rewrite the CountOccurrences procedure shown earlier in this chapter. Before doing so, bear in mind that the CountOccurrences procedure counts every occurrence of a particular character in a string, even if there is more than one occurrence in a word. When you are using the Filter function, on the other hand, you can count an occurrence only once per element, even if the character occurs twice within a single element in the array.

For examples that demonstrate the Filter function, see "Converting Strings" and "Searching an Array" later in this chapter.