将字符串与模式进行比较。
重要
. Like
NET Core 和 .NET Standard 项目中当前不支持该运算符。
语法
result = string Like pattern
部件
result
必填。 任何 Boolean
变量。 结果是一个Boolean
值,该值指示是否string
满足 。pattern
string
必填。 任何 String
表达式。
pattern
必填。
String
任何符合“备注”中所述模式匹配约定的表达式。
注解
如果满足包含pattern
result
的模式的值string
是True
。 如果字符串不满足模式, result
则为 False
。 如果两者均为string
pattern
空字符串,则结果为 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
包含范围中的任何字符,Z
则会导致匹配;如果[!H–L]
相应的字符位置包含范围A
H
之外的任何字符,L
则会导致匹配。
指定一系列字符时,它们必须按升序排序顺序显示,即从最低到最高。 因此, [A–Z]
是一种有效的模式,但 [Z–A]
不是。
多个字符范围
若要为同一字符位置指定多个范围,请将它们放在同一个括号中,而不使用分隔符。 例如,[A–CX–Z]
如果相应的字符位置string
包含范围(或X
范围 A
C
–Z
)中的任何字符,则会导致匹配。
连字符的用法
连字符(–
)可以出现在开头(感叹号(如果有)或匹配自身的末尾 charlist
。 在任何其他位置,连字符标识由连字符任一端的字符分隔的字符范围。
排序规则序列
指定范围的含义取决于运行时的字符排序,由 Option Compare
运行代码的系统的区域设置和区域设置确定。 其中Option Compare Binary
,范围[A–E]
匹配A
、B
、C
和D
E
。 具有Option Compare Text
、匹配A
项[A–E]
、a
、、à
C
D
c
d
B
b
À
和。 E
e
范围不匹配 Ê
,或者 ê
因为重音字符按排序顺序排序后不区分字符排序。
日记字符
在某些语言中,有一些字母字符表示两个单独的字符。 例如,多种语言使用字符 æ
来表示字符 a
以及 e
它们一起出现的时间。 运算符 Like
可识别单个日记字符和两个单独的字符是等效的。
当在系统区域设置中指定使用日记字符的语言时,单一日记字符的 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*"