Like 运算符 (Visual Basic)Like Operator (Visual Basic)

将字符串与模式进行比较。Compares a string against a pattern.

重要

Like.Net Core 和 .NET Standard 项目目前不支持运算符。The Like operator is currently not supported in .NET Core and .NET Standard projects.

语法Syntax

result = string Like pattern  

组成部分Parts

result
必需。Required. 任何 Boolean 变量。Any Boolean variable. 结果是一个 Boolean 值,该值指示是否 string 满足 patternThe result is a Boolean value indicating whether or not the string satisfies the pattern.

string
必需。Required. 任何 String 表达式。Any String expression.

pattern
必需。Required. String符合 "备注" 中所述的模式匹配约定的任何表达式。Any String expression conforming to the pattern-matching conventions described in "Remarks."

备注Remarks

如果中的值 string 满足中包含的模式 patternresult 则为 TrueIf the value in string satisfies the pattern contained in pattern, result is True. 如果字符串不满足模式, result 则为 FalseIf the string does not satisfy the pattern, result is False. 如果 stringpattern 都为空字符串,则结果为 TrueIf both string and pattern are empty strings, the result is True.

比较方法Comparison Method

运算符的行为 Like 取决于 Option Compare 语句The behavior of the Like operator depends on the Option Compare Statement. 每个源文件的默认字符串比较方法为 Option Compare BinaryThe default string comparison method for each source file is Option Compare Binary.

模式选项Pattern Options

内置模式匹配为字符串比较提供了多种工具。Built-in pattern matching provides a versatile tool for string comparisons. 使用模式匹配功能,可以将中的每个字符与 string 特定字符、通配符、字符列表或字符范围匹配。The pattern-matching features allow you to match each character in string against a specific character, a wildcard character, a character list, or a character range. 下表显示了中允许的字符 pattern 以及它们匹配的内容。The following table shows the characters allowed in pattern and what they match.

字符 patternCharacters in pattern 匹配项 stringMatches in string
? 任何单个字符Any single character
* 零个或多个字符Zero or more characters
# 任何单个数字 (0 – 9) Any single digit (0–9)
[charlist] 中的任何单个字符 charlistAny single character in charlist
[!charlist] 不在中的任何单个字符 charlistAny single character not in charlist

字符列表Character Lists

括在括号中的一个或多个字符组 (charlist) ([ ]) 可用于匹配中的任何单个字符 string ,并可包括几乎所有字符代码(包括数字)。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 any character code, including digits.

开始时 () 的感叹号 ! 表示在中 charlist 找到除中的字符以外的任何字符时进行匹配 charlist stringAn 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.

特殊字符Special Characters

若要匹配特殊字符左方括号 ([) 、问号 (?) 、数字符号 () # 和星号 () * ,请将它们括在括号中。To match the special characters left bracket ([), question mark (?), number sign (#), and asterisk (*), enclose them in brackets. ]不能在组内使用右方括号 () ,而是可以将其作为单个字符在组外使用。The right bracket (]) cannot be used within a group to match itself, but it can be used outside a group as an individual character.

字符序列 [] 被视为 () 长度为零的字符串 ""The character sequence [] is considered a zero-length string (""). 但是,它不能是括在括号中的字符列表的一部分。However, it cannot be part of a character list enclosed in brackets. 如果要检查中的某个位置是否 string 包含一组字符或不包含任何字符,可以使用 Like 两次。If you want to check whether a position in string contains one of a group of characters or no character at all, you can use Like twice. 有关示例,请参阅 如何:将字符串与模式匹配For an example, see How to: Match a String against a Pattern.

字符范围Character Ranges

通过使用连字符 () 来分隔范围的下限和上限, charlist 可以指定字符范围。By using a hyphen () to separate the lower and upper bounds of the range, charlist can specify a range of characters. 例如, [A–Z] 如果中的相应字符位置 string 包含范围内的任何字符,则会导致匹配 A Z ; [!H–L] 如果相应的字符位置包含范围之外的任何字符, H L 则结果将生成匹配项。For example, [A–Z] results in a match if the corresponding character position in string contains any character within the range AZ, and [!H–L] results in a match if the corresponding character position contains any character outside the range HL.

指定字符范围时,它们必须按升序排序,即从最低到最高。When you specify a range of characters, they must appear in ascending sort order, that is, from lowest to highest. 因此, [A–Z] 是有效的模式,但 [Z–A] 不是。Thus, [A–Z] is a valid pattern, but [Z–A] is not.

多字符范围Multiple Character Ranges

若要为同一字符位置指定多个范围,请将它们放在不带分隔符的相同括号中。To specify multiple ranges for the same character position, put them within the same brackets without delimiters. 例如, [A–CX–Z] 如果中的相应字符位置 string 包含范围或范围内的任何字符,则会导致 A 匹配 C X ZFor example, [A–CX–Z] results in a match if the corresponding character position in string contains any character within either the range AC or the range XZ.

连字符的用法Usage of the Hyphen

连字符 () 可以出现在感叹号后的开头 (,如果有任何) 或末尾,则 charlist 为。A hyphen () can appear either at the beginning (after an exclamation point, if any) or at the end of charlist to match itself. 在其他任何位置,连字符标识由连字符两侧的字符分隔的一系列字符。In any other location, the hyphen identifies a range of characters delimited by the characters on either side of the hyphen.

排序顺序Collating Sequence

指定范围的含义取决于运行时的字符排序,由确定, Option Compare 以及运行代码的系统的区域设置。The meaning of a specified range depends on the character ordering at run time, as determined by Option Compare and the locale setting of the system the code is running on. 对于 Option Compare Binary ,范围 [A–E] 匹配、、、 A B C DEWith Option Compare Binary, the range [A–E] matches A, B, C, D, and E. With、匹配、、、、、、、、、、 Option Compare Text [A–E] A a À à B b C c D d EeWith Option Compare Text, [A–E] matches A, a, À, à, B, b, C, c, D, d, E, and e. 该范围不匹配, Ê 也不是 ê 因为重音字符在排序顺序中逐重音字符排序。The range does not match Ê or ê because accented characters collate after unaccented characters in the sort order.

连字符Digraph Characters

在某些语言中,有表示两个不同字符的字母字符。In some languages, there are alphabetic characters 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 digraph character and the two individual characters are equivalent.

如果在系统区域设置中指定了使用连字符的语言,则或中的单个连字符的出现位置 pattern string 与另一个字符串中的等效双字符序列匹配。When a language that uses a digraph character is specified in the system locale settings, an occurrence of the single digraph character in either pattern or string matches the equivalent two-character sequence in the other string. 同样, pattern 括在括号中的一条带区字符 (自身、列表或范围中) 与中等效的双字符序列匹配 stringSimilarly, a digraph character in pattern enclosed in brackets (by itself, in a list, or in a range) matches the equivalent two-character sequence in string.

重载Overloading

Like运算符可以重载,这意味着当操作数具有该类或结构的类型时,该类或结构可以重新定义其行为。The Like operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. 如果你的代码在该类或结构上使用此运算符,请确保了解其重新定义的行为。If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. 有关详细信息,请参阅 Operator ProceduresFor more information, see Operator Procedures.

示例Example

此示例使用 Like 运算符将字符串与各种模式进行比较。This example uses the Like operator to compare strings to various patterns. 结果将进入一个 Boolean 变量,该变量指示每个字符串是否满足此模式。The results go into a Boolean variable indicating whether each string satisfies the pattern.

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*"

另请参阅See also