Like 運算子
比較子 | InStr 函數 | Visual Basic 中的運算子優先順序 | 運算子(按功能列出) | Option Compare 語句 | StrComp 函數 | 運算子
比較兩個字串。
result = string Like pattern
各部分說明
result
必選項。任何 Boolean 變數。結果是 Boolean 值,它表示 string 是否匹配 pattern。
string
必選項。任何 String 運算式。
pattern
必選項。任何符合“備忘”中描述的模式比對約定的 String 運算式。
備忘
如果 string 匹配 pattern,則 result 為 True;如果不匹配,則 result 為 False。如果 string 和 pattern 都是Null 字元串,則結果是 True。否則,如果 string 或 pattern 中有一個為空白字串,則結果為 False。
Like 運算子的行為取決於 Option Compare 語句。每個模組的預設字串比較方法是 Option Compare Binary。
內建的模式比對為字串比較提供了一種多功能工具。模式比對功能允許您使用萬用字元、字元列表或字元範圍的任何組合來匹配字串。下表顯示 pattern 中允許的字元和這些字元的匹配項:
pattern 中的字元 string 中的匹配項
? 任何單個字元
* 零或更多字元
# 任何單個數字(0 到 9)
[charlist] charlist 中的任何單個字元
[!charlist] 不在 charlist 中的任何單個字元
括在方括弧 ([ ]) 內的一個或多個字元的組 (charlist) 可以用於匹配 string 中的任何單個字元,並且可以包含幾乎任何字元代碼(包括數字)。
注意 若要匹配特殊字元,如左方括弧 ([)、問號 (?)、數字記號 (#) 和星號 (*),請將它們括在方括弧中。不能在組中使用右方括弧 (]) 來匹配它自己,但可以在組外作為單個字元使用。
通過使用連字號 (–) 將範圍的上下限分開,charlist 可以指定字元的範圍。例如,如果 string 中的相應字元位置包含範圍 A–Z 中的任何大寫字母,則 [A-Z] 將產生匹配。可以在方括弧中包括多個範圍而不用分隔字元。
指定範圍的含義取決於在運行時有效字元排序(由 Option Compare 和運行代碼的系統的地區設定確定)。當使用 Option Compare Binary 時,範圍 [A–E] 匹配 A、B、C、D 和 E。當使用 Option Compare Text 時,[A–E] 匹配 A、a、à、à、B、b、C、c、D、d、E 和 e。該範圍不匹配 ê 或 ê,因為在排序次序中,重音字元排在非重音字元之後。
模式比對的其他重要規則包括:
charlist 開始處的驚嘆號 (!) 意味著僅當在 string 中找到除 charlist 以外的任何字元時才發生匹配。當在方括弧外使用時,驚嘆號匹配它自己。
連字號 (-) 可以出現在 charlist 的開始處(如果有驚嘆號,則在它後面)或結尾處以匹配它自己。在任何其他位置,連字號標識由連字號兩側的字元界定的字元範圍。
在指定字元範圍時,這些字元必須以升序排序次序出現(即,從最低到最高)。因此,[A-Z] 是有效模式,但 [Z-A] 不是。
字元序列 [] 被視為零長度字串 ("")。
在某些語言中,字母表中有一些表示兩種不同字元的特殊字元。例如,有幾種語言使用字元 ? 來表示字元 a 和 e(當這兩個字元一起出現時)。Like 運算子認為該單個特殊字元與這兩個字元是等效的。
當在系統地區設定中指定使用特殊字元的語言時,在 pattern 或 string 中出現的單個特殊字元都匹配其他字串中等效的雙字元序列。與此類似,括在方括弧內的 pattern 中的單個特殊字元(獨立存在、在列表中或在某個範圍內)匹配 string 中等效的雙字元序列。
樣本
本樣本使用 Like 運算子將字串與模式比較。結果是 Boolean 值,它表示該字串是否符合該模式。
Dim myCheck As Boolean
myCheck = "F" Like "F" ' Does "F" match "F"? Returns True.
myCheck = "F" Like "f" ' Does "F" match "f"? Returns False.
myCheck = "F" Like "FFF" ' Does "F" match "FFF"? Returns False.
myCheck = "aBBBa" Like "a*a" ' Does "aBBBa" have a "a" at the
' beginning, an "a" at the end, and any number of characters in
' between? Returns True.
myCheck = "F" Like "[A-Z]" ' Does "F" occur in the set of
' characters from A to Z? Returns True.
myCheck = "F" Like "[!A-Z]" ' Does "F" NOT occur in the set of
' characters from A to Z? Returns False.
myCheck = "a2a" Like "a#a" ' Does "a2a" begin and end with an
' "a" and have any single-digit number inbetween? Returns True.
myCheck = "aM5b" Like "a[L-P]#[!c-e]" ' Does "aM5b" fit the following
' pattern: Begins with "a", has and character from the set L through
' P, followed byb any single-digit number, and finally contains any
' character excluded from the character set c through e. Returns True.
myCheck = "BAT123khg" Like "B?T*" ' Does "BAT123khg" fit the
' following pattern: Begins with "B", followed by any single
' character, followed by a "T" and finally zero or more characters
' of any type. Returns True.
myCheck = "CAT123khg" Like "B?T*" ' Does "CAT123khg" fit the
' following pattern: Begins with "B", followed by any single
' character, followed by a "T" and finally zero or more characters
' of any type. Returns False.
Option Compare 語句請參見
比較子 | InStr 函數 | InStrRev 函數 | Option Strict 語句 | Option Explicit 語句 | Replace 函數 | Split 函數 | StrComp 函數 | /optionexplicit | /optionstrict | /optioncompare:binary | /optioncompare:text
用於在檔案級聲明當比較字串資料時所使用的預設比較方法。
Option Compare { Binary | Text }
各部分說明
Binary
可選項。導致基於從字元的內部二進位表示形式匯出的排序次序進行字串比較。
Text
可選項。導致基於由系統的地區設定確定的不區分大小寫文本排序次序進行字串比較。
備忘
如果使用,則 Option Compare 語句必須出現在檔案中其他任何源語句之前。
Option Compare 語句為類、模組或結構指定字串比較方法(Binary 或 Text)。如果未包括 Option Compare 語句,則預設的文本比較方法是 Binary。
在 Microsoft Windows 中,排序次序由字碼頁確定。在下面的樣本中,使用 Option Compare Binary 對字元進行排序,將產生典型的二進位排序次序:
A < B < E < Z < a < b < e < z < à < ê < ? < à < ê < ?
如果使用 Option Compare Text 對同樣的字元進行排序,將產生下面的文本排序次序:
(A=a) < (à = à) < (B=b) < (E=e) < (ê = ê) < (Z=z) < (? = ?)
樣本
本樣本使用 Option Compare 語句設定預設的字串比較方法。只能在模組層級使用 Option Compare 語句。
' Set the string comparison method to Binary.
Option Compare Binary ' That is, "AAA" is less than "aaa".
' Set the string comparison method to Text.
Option Compare Text ' That is, "AAA" is equal to "aaa".
請參見
比較子 | InStr 函數 | InStrRev 函數 | Option Strict 語句 | Option Explicit 語句 | Replace 函數 | Split 函數 | StrComp 函數 | /optionexplicit | /optionstrict | /optioncompare:binary | /optioncompare:text