Like 運算子 (Visual Basic)

比較字串和模式。

重要

.NET Core 和 .NET Standard 專案目前不支援 Like 運算子。

語法

result = string Like pattern  

組件

result
必要。 任何Boolean 變數。 結果為 Boolean 值,表示 string 是否符合 pattern 的需求。

string
必要。 任何 String 運算式。

pattern
必要。 任何符合「備註」所述模式比對慣例的 String 運算式。

備註

如果 string 中的值符合 pattern 所包含模式的需求,result 便為 True。 如果字串不符合模式需求,則 resultFalse。 如果 stringpattern 都是空字串,則結果為 True

比較的方法

Like 運算子的行為取決於 Option Compare 陳述式。 每個來源檔案的預設字串比較方法為 Option Compare Binary

模式選項

內建模式比對提供可進行字串比較的多用途工具。 模式比對功能可讓您將 string 中的每個字元與特定字元、萬用字元、字元清單或字元範圍進行比對。 下表顯示可在 pattern 中使用的字元以及這些字元符合的項目。

pattern中的字元 string中的相符項目
? 任何單一字元
* 零或多個字元
# 任何單一數字 (0–9)
[charlist] charlist 中的任何單一字元
[!charlist] 不在 charlist 中的任何單一字元

字元清單

一組以括弧 ([ ]) 括住的一或多個字元 (charlist) 可用來比對 string 中的任何單一字元,而且幾乎可以包含任何字元碼,包括數字。

charlist 開頭的驚嘆號 (!) 表示,如果在 string 中找到 charlist 內字元以外的任何字元,便會進行比對。 驚嘆號若放在括弧外面,該驚嘆號會和自身進行比對。

特殊字元

若要比對特殊字元左括弧 ([)、問號 (?)、數字符號 (#) 和星號 (*),請用括弧括住它們。 右括弧 (]) 不能在群組中用來比對本身,但可以作為個別字元在群組外部使用。

系統會將字元序列 [] 視為長度為零的字串 (""), 然而,該字串不能列在以括弧括住的字元清單中。 如果您想要檢查 string 中的某個位置是否包含其中一組字元或完全沒有字元,可以使用 Like 兩次。 如需範例,請參閱操作說明:比對字串和模式

字元範圍

使用連字號 () 來分隔範圍的下限和上限,charlist 便可以指定字元範圍。 舉例來說,如果 string 中的對應字元位置包含範圍 AZ中的任何字元,[A–Z] 將產生相符項目,如果對應的字元位置包含範圍 HL 以外的任何字元,則[!H–L] 會產生相符項目。

指定字元範圍時,這些字元必須以遞增排列順序顯示,意即從最低到最高, 因此,[A–Z] 是有效的模式,但 [Z–A] 不是。

多個字元範圍

若要為相同字元位置指定多個範圍,請將這些範圍置於沒有分隔符號的相同括弧內。 舉例來說,如果 string 中的對應字元位置包含 AC 範圍,或 XZ 範圍內的任何字元,[A–CX–Z] 將產生相符項目。

連字號使用方式

連字號 () 可能會出現在 charlist 的開頭 (如有驚嘆號的話,會在驚嘆號之後) 或結尾 (和自身進行比對)。 在其他任何位置中,連字號將用來指出連字號任一端字元所界定的字元範圍。

定序排列

根據 Option Compare,以及程式碼執行所在系統的地區設定,指定範圍的意義取決於執行階段的字元順序。 執行 Option Compare Binary,範圍 [A–E] 便與 ABCDE 相符。 執行 Option Compare Text[A–E] 便與 AaÀàBbCcDdEe 相符。 範圍不符合 Êê 是因為在排列順序中,有重音符號的字元會排在沒有重音符號的字元後面。

二合字元

某些語言具有代表兩個不同字元的字母字元。 舉例來說,有多種語言使用 æ 字元來表示一起出現的 ae 字元。 Like 運算子可識別一個二合字母字元等於兩個個別字元。

若系統的地區設定指定了使用二合字元的語言,只要 patternstring 中出現一個二合字元都符合另一個字串中的對等雙字元順序。 同樣地,以括弧括住的 pattern (無論只有本身,或是位於清單或範圍內) )中的二合字元符合 string 中的對等雙字元順序。

多載化

Like 運算子可能會多載,這表示若運算元具有類別或結構的型別,該類別或結構可以重新定義其行為。 如果您的程式碼在這類類別或結構上使用此運算子,請務必了解其重新定義的行為。 如需詳細資訊,請參閱 Operator Procedures

範例

此範例使用 Like 運算子來將字串與各種模式進行比較。 結果會進入 Boolean 變數,指出每個字串是否符合模式需求。

Dim testCheck As Boolean
' The following statement returns True (does "F" satisfy "F"?)
testCheck = "F" Like "F"
' The following statement returns False for Option Compare Binary
'    and True for Option Compare Text (does "F" satisfy "f"?)
testCheck = "F" Like "f"
' The following statement returns False (does "F" satisfy "FFF"?)
testCheck = "F" Like "FFF"
' The following statement returns True (does "aBBBa" have an "a" at the
'    beginning, an "a" at the end, and any number of characters in 
'    between?)
testCheck = "aBBBa" Like "a*a"
' The following statement returns True (does "F" occur in the set of
'    characters from "A" through "Z"?)
testCheck = "F" Like "[A-Z]"
' The following statement returns False (does "F" NOT occur in the 
'    set of characters from "A" through "Z"?)
testCheck = "F" Like "[!A-Z]"
' The following statement returns True (does "a2a" begin and end with
'    an "a" and have any single-digit number in between?)
testCheck = "a2a" Like "a#a"
' The following statement returns True (does "aM5b" begin with an "a",
'    followed by any character from the set "L" through "P", followed
'    by any single-digit number, and end with any character NOT in
'    the character set "c" through "e"?)
testCheck = "aM5b" Like "a[L-P]#[!c-e]"
' The following statement returns True (does "BAT123khg" begin with a
'    "B", followed by any single character, followed by a "T", and end
'    with zero or more characters of any type?)
testCheck = "BAT123khg" Like "B?T*"
' The following statement returns False (does "CAT123khg"?) begin with
'    a "B", followed by any single character, followed by a "T", and
'    end with zero or more characters of any type?)
testCheck = "CAT123khg" Like "B?T*"

另請參閱