比較字串與模式。
這很重要
.NET Core 和 .NET Standard 專案中目前不支援運算符 Like 。
語法
result = string Like pattern
組件
result
必須的。 任何 Boolean 變數。 結果是值 Boolean ,指出是否符合 stringpattern。
string
必須的。 任何 String 表達式。
pattern
必須的。 任何 String 符合「備註」中所述模式比對慣例的表達式。
備註
如果的 string 值符合 中包含的 pattern模式, result 則為 True。 如果字串不符合模式, result 則為 False。 如果與 pattern 都是string空字串,則結果為 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 可以指定字元範圍。 例如,[A–Z]如果中的string對應字元位置包含範圍ZA中的任何字元,則會產生相符的結果;如果[!H–L]對應的字元位置包含範圍HL以外的任何字元,則會產生相符的結果。
當您指定字元範圍時,它們必須以遞增排序順序顯示,也就是從最低到最高。 因此, [A–Z] 是有效的模式,但 [Z–A] 不是。
多個字元範圍
若要指定相同字元位置的多個範圍,請將這些範圍放在沒有分隔符的相同括弧內。 例如, [A–CX–Z] 如果中的 string 對應字元位置包含範圍 A內的任何字元,C 或範圍 X–Z ,則會產生相符的結果。
連字元的使用方式
連字元 (–) 可以出現在開頭(在驚嘆號之後,如果有的話)或以符合本身的結尾 charlist 。 在任何其他位置中,連字元會識別以連字元任一端字元分隔的字元範圍。
定序序列
指定範圍的意義取決於運行時間的字元順序,如程式代碼執行之系統的地區設定所決定 Option Compare 。 使用 Option Compare Binary時,範圍 [A–E] 會比對 A、 B、 C、 D、 和 E。 搭配 Option Compare Text、[A–E]比對 A、a、、、À、CDcàBb、d、、 E和 。e 此範圍不相符 Ê ,或 ê 因為輔色字元在排序順序中未存取的字元之後定序。
日記字元
在某些語言中,有代表兩個不同的字元的字母字元。 例如,數種語言會使用 字元 æ 來表示字元 a ,以及 e 它們一起出現時。 運算子 Like 可辨識單一日記字元和兩個個別字元相等。
當系統地區設定中指定使用 digraph 字元的語言時,其中一個單一日記字元 pattern 的出現,或 string 符合另一個字元串中的對等雙字元序列。 同樣地,以括弧括住的日記字元 pattern (本身、在清單中或範圍中)符合 中的 string對等雙字元序列。
重載
Like運算子可以多載,這表示當作數具有該類別或結構的類型時,類別或結構可以重新定義其行為。 如果您的程式代碼在這類類別或結構上使用這個運算符,請務必瞭解其重新定義的行為。 如需詳細資訊,請參閱 運算符程式。
範例
這個範例會 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*"