Filter 函数 (Visual Basic)

更新:2007 年 11 月

返回一个从零开始的数组,该数组包含基于指定筛选条件的 String 数组的子集。

Function Filter(
   ByVal Source() As { Object | String },
   ByVal Match As String,
   Optional ByVal Include As Boolean = True,
   Optional ByVal Compare As CompareMethod = CompareMethod.Binary
)  As String()

参数

  • Source
    必选。要搜索的一维字符串数组。

  • Match
    必选。要搜索的字符串。

  • Include
    可选。Boolean 值,指示返回的子字符串是否包含 Match。如果 Include 为 True,则 Filter 函数返回将 Match 作为子字符串包含在其中的数组的子集。如果 Include 为 False,则 Filter 函数返回不将 Match 作为子字符串包含在其中的数组的子集。

  • Compare
    可选。表示要使用的字符串比较类型的数值。请参见“设置”了解具体的值。

设置

Compare 参数可以具有下列值。

常数

说明

CompareMethod.Binary

执行二进制比较

CompareMethod.Text

执行文本比较

异常

异常类型

错误号

条件

ArgumentException

9

Source 为 Nothing 或不是一维数组。

如果正在升级使用无结构错误处理的 Visual Basic 6.0 应用程序,请参见“错误号”一列。(您可以根据 Number 属性(Err 对象)比较错误号。)然而,如果可能,应当考虑用 Visual Basic 的结构化异常处理概述替换这种错误控制。

备注

如果在 Source 中未找到 Match 的匹配项,Filter 函数将返回一个空数组。如果将 Source 设置为 Nothing 或者它不是一维数组,将发生错误。

Filter 函数返回的数组中包含的元素数目足以容纳匹配项数目。

示例

此示例演示 Filter 函数的用法。

Dim TestStrings(2) As String
TestStrings(0) = "This"
TestStrings(1) = "Is"
TestStrings(2) = "It"
Dim subStrings() As String
' Returns ["This", "Is"].
subStrings = Filter(TestStrings, "is", True, CompareMethod.Text)
' Returns ["This"].
subStrings = Filter(TestStrings, "is", True, CompareMethod.Binary)
' Returns ["Is", "It"].
subStrings = Filter(TestStrings, "is", False, CompareMethod.Binary)

要求

命名空间:Microsoft.VisualBasic

**模块:**Strings

**程序集:**Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)

请参见

参考

字符串操作摘要

Replace 函数 (Visual Basic)

ArgumentException