Visual Basic Language Reference  

Like Operator

Compares two strings.

result = string Like pattern

Parts

result
Required. Any Boolean variable. The result is a Boolean value indicating whether or not the string matches the pattern.
string
Required. Any String expression.
pattern
Required. Any String expression conforming to the pattern-matching conventions described in Remarks.

Remarks

If string matches pattern, result is True; if there is no match, result is False. If both string and pattern are an empty string. the result is True. Otherwise, if either string or pattern is an empty string, the result is False.

The behavior of the Like operator depends on the Option Compare statement. The default string-comparison method for each module is Option Compare Binary.

Built-in pattern matching provides a versatile tool for string comparisons. The pattern-matching features allow you to match strings using wildcard characters, character lists, or character ranges, in any combination. 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
# Any single digit (0–9)
[charlist] Any single character in charlist
[!charlist] Any single character not in 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 any character 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.

By using a hyphen () to separate the upper and lower bounds of the range, charlist can specify a range of characters. 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 the locale setting of the system the code is running on). Using Option Compare Binary, the range [A–E] matches A, B, C, D, and E. With 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 fall after unaccented characters in the sort order.

Other important rules for pattern matching include the following:

In some languages, there are special characters in the alphabet that represent two separate characters. For example, several languages use the character ? to represent the characters a and e when they appear together. The Like operator recognizes that the single special character and the two individual characters are equivalent.

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 two-character sequence in the other string. Similarly, a single special character in pattern enclosed in brackets (by itself, in a list, or in a range) matches the equivalent two-character sequence in string.

Example

This example uses the Like operator to compare a string to a pattern. The result is a Boolean value representing whether the string fits the pattern.

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.

See Also

Comparison Operators | InStr Function | Operator Precedence in Visual Basic | Operators Listed by Functionality | Option Compare Statement | StrComp Function | Operators