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)

需求

命名空間 (Namespace)︰Microsoft.VisualBasic

模組:Strings

組件 (Assembly):Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)

請參閱

參考

字串操作摘要

Replace 函式 (Visual Basic)

ArgumentException