Table of contents
TOC
折叠目录
展开目录

与运算符类似Like Operator

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

用于比较两个字符串。Used to compare two strings.

语法Syntax

结果=字符串模式result=stringLikepattern

运算符语法包含以下成分:The Like operator syntax has these parts:

部件Part说明Description
resultresult所需;任何数值变量Required; any numeric variable.
stringstring所需;任何字符串表达式Required; any string expression.
模式pattern必需;任何符合备注中所述的模式匹配约定的字符串表达式。Required; any string expression conforming to the pattern-matching conventions described in Remarks.

备注Remarks

如果_字符串_与_模式_匹配,结果_为True;如果没有匹配项,则_结果_为FalseIf _string matches pattern, result is True; if there is no match, result is False.如果Null字符串_或_图案,则_结果_为NullIf either string or pattern isNull, result is Null.
Like运算符的行为取决于比较选项的语句。The behavior of the Like operator depends on the Option Compare statement.每个模块的默认字符串比较方法是比较选项二进制The defaultstring-comparison method for eachmodule is Option CompareBinary.

比较二进制选项会导致基于从字符的内部二进制表示形式派生的排序顺序的字符串比较。Option Compare Binary results in string comparisons based on asort order derived from the internal binary representations of the characters.排序顺序取决于代码页。Sort order is determined by the code page.在下面的示例显示了典型的二进制排序顺序:In the following example, a typical binary sort order is shown:
A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < øA < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø

选项比较文本会导致基于由系统的区域设置确定的不区分大小写的文本排序顺序进行字符串比较。Option Compare Text results in string comparisons based on a case-insensitive, textual sort order determined by your system'slocale.排序时使用选项比较文本的字符相同,将产生以下的文本排序顺序:When you sort the same characters using Option Compare Text, the following text sort order is produced:
(A=a) < (À=à) < (B=b) < (E=e) < (Ê=ê) < (Z=z) < (Ø=ø)(A=a) < (À=à) < (B=b) < (E=e) < (Ê=ê) < (Z=z) < (Ø=ø)

内置的模式匹配的字符串比较提供的多用途工具。Built-in pattern matching provides a versatile tool for string comparisons.模式匹配功能允许您使用通配符字符、 字符列表或字符范围的任意组合,以匹配字符串。The pattern-matching features allow you to use wildcard characters, character lists, or character ranges, in any combination, to match strings.下表显示了允许的_模式_和它们的匹配的字符:The following table shows the characters allowed in pattern and what they match:

_模式_中的字符Characters in pattern在_字符串_中的匹配项Matches in string
?任意单个字符。Any single character.
\*零个或多个字符。Zero or more characters.
#任何单个数字 (0-9)。Any single digit (0-9).
[字符列表][ charlist ]字符列表_中的任何单个字符。Any single character in _charlist.
[ !字符列表][ !charlist ]不在_字符列表_中的任意单个字符。Any single character not in charlist.

一组一个或多个字符 ( charlist ) 括在方括号 ( [] ) 可以用于匹配_字符串_中的任何单个字符,可以包含几乎任何字符的代码,包括数字。A group of one or more characters ( charlist ) enclosed in brackets ( [ ] ) can be used to match any single character in string and can include almost anycharacter code, including digits.

请注意 特殊字符左的括号匹配 ( [ ),问号 ( Note To match the special characters left bracket ( [ ), question mark ( ?),数字记号 ( # ),和星号 ( \* ),将它们括在中括号中。), number sign ( # ), and asterisk ( \* ), enclose them in brackets.右方括号 ( ] ) 不能在组中用来与自身匹配,但它可用在组外作为单个字符。The right bracket ( ] ) can't be used within a group to match itself, but it can be used outside a group as an individual character.

通过使用连字符 ( - ) 来分隔范围的上限和下限边界, charlist_可以指定字符的范围。By using a hyphen ( - ) to separate the upper and lower bounds of the range, _charlist can specify a range of characters.例如,[A-Z]结果匹配,如果_字符串_中的相应字符位置包含任何大写字母 A 到 Z 的范围中。For example, [A-Z] results in a match if the corresponding character position in string contains any uppercase letters in the range A-Z.包括没有分隔符方括号内的多个区域。Multiple ranges are included within the brackets without delimiters.指定范围的含义取决于排序在运行时(如通过比较选项和系统运行该代码的区域设置来确定) 的有效的字符。The meaning of a specified range depends on the character ordering valid at run time (as determined by Option Compare and thelocale setting of the system the code is running on).使用选项比较二进制的示例中,该区域[A-E]匹配 A,B 和 e。用选项比较文本[A-E]匹配 A、 a、 À,à,B,b、 E、 e。Using the Option Compare Binary example, the range [A-E] matches A, B and E. With Option Compare Text, [A-E] matches A, a, À, à, B, b, E, e.该范围不匹配 Ê 因为非重音字符的排序顺序之后都不带重音符号的字符。The range does not match Ê or ê because accented characters fall after unaccented characters in the sort order.模式匹配的其他重要规则包括:Other important rules for pattern matching include the following:

  • 感叹号 ( An exclamation point ( !) 在_字符串_中,字符列表_中的字符以外的任何字符,则将生成一个匹配的方法找到_字符列表_的开头。) at the beginning of _charlist means that a match is made if any character except the characters in charlist is found in string.当使用方括号外面时,感叹号匹配其本身。When used outside brackets, the exclamation point matches itself.

  • 一个连字符 ( - ) 可以显示 (如果使用了感叹号点) 后的开头或末尾以便与其自身匹配_字符列表_。A hyphen ( - ) can appear either at the beginning (after an exclamation point if one is used) or at the end of charlist to match itself.在任何其他位置,连字符用于标识某个范围的字符。In any other location, the hyphen is used to identify a range of characters.

  • 当指定范围的字符,则它们必须出现在升序排序次序 (从最低到最高)。When a range of characters is specified, they must appear in ascending sort order (from lowest to highest).[A-Z]是一种有效模式,但[Z-A]不是。[A-Z] is a valid pattern, but [Z-A] is not.

  • 字符序列[]被认为是一个零长度字符串 ("")。The character sequence [] is considered a zero-length string ("").

在某些语言中,有一些字母表中代表两个字符的特殊字符。In some languages, there are special characters in the alphabet that represent two separate characters.例如,几种语言都使用字符"æ"来表示字符"a"和"e",当他们出现在一起。For example, several languages use the character "æ" to represent the characters "a" and "e" when they appear together.Like运算符认为单个特殊字符与这两个字符是等效。The Like operator recognizes that the single special character and the two individual characters are equivalent.系统区域设置中指定使用的特殊字符的语言时,在_pattern_或_string_中的单个特殊字符的匹配项匹配其他字符串中等效的 2 个字符序列。When a language that uses a special character is specified in the system locale settings, an occurrence of the single special character in either pattern or string matches the equivalent 2-character sequence in the other string.与此类似,括在方括号内_模式_中的单个特殊字符 (独立存在、 在列表中,或在范围内) 匹配_string_中等效的 2 个字符序列。Similarly, a single special character in pattern enclosed in brackets (by itself, in a list, or in a range) matches the equivalent 2-character sequence in string.

示例Example

本示例使用Like运算符将字符串与模式进行比较。This example uses the Like operator to compare a string to a pattern.

Dim MyCheck
MyCheck = "aBBBa" Like "a*a"    ' Returns True.
MyCheck = "F" Like "[A-Z]"    ' Returns True.
MyCheck = "F" Like "[!A-Z]"    ' Returns False.
MyCheck = "a2a" Like "a#a"    ' Returns True.
MyCheck = "aM5b" Like "a[L-P]#[!c-e]"    ' Returns True.
MyCheck = "BAT123khg" Like "B?T*"    ' Returns True.
MyCheck = "CAT123khg" Like "B?T*"    ' Returns False.
MyCheck = "ab" Like "a*b"    ' Returns True.
MyCheck = "a*b" Like "a[*]b"    ' Returns True.
MyCheck = "axxxxxb" Like "a[*]b"    ' Returns False.
MyCheck = "a[xyz" Like "a[[]*"    ' Returns True.
MyCheck = "a[xyz" Like "a[*"    ' Throws Error 93 (invalid pattern string).
© 2018 Microsoft