Filter - VBScript


Overview

The Filter() function in VBScript allows you to search through a string or array and extract specific elements based on a specified condition. It is commonly used for data filtering, searching, and text manipulation tasks.

Syntax

Filter array | object, condition, [matchCase | ignoreCase]

Parameters:

  • array | object: The array or string to be filtered.
  • condition: The expression used to filter the elements.
  • matchCase (optional): True or False, specifies case-sensitive or case-insensitive comparison. Defaults to True.
  • ignoreCase (optional): True or False, specifies case-insensitive comparison. Defaults to False.

Options/Flags

  • matchCase: Controls the case sensitivity of the filter expression.
  • ignoreCase: Same as matchCase but explicitly sets the comparison to be case insensitive.

Examples

' Filter an array based on a numerical condition
arr = [1, 2, 3, 4, 5]
result = Filter(arr, "item > 3")

' Filter a string based on a substring
str = "Hello World"
result = Filter(str, "item = 'World'")

' Case-insensitive filter
result = Filter(str, "item = 'WORLD'", False)

Common Issues

  • Invalid array or string: Ensure that the first parameter is a valid array or string.
  • Invalid condition: The condition expression must be a valid VBScript expression.
  • No matches found: If no matches are found, an empty array or string is returned.

Integration

Filter() can be combined with other VBScript functions or objects, such as:

' Create an array of cities and filter based on a substring
cities = ["London", "New York", "Paris"]
filteredCities = Filter(cities, "item Like 'Pari*'")
  • InStr()
  • Replace()
  • Split()