Like Operator

Used to compare two strings.

Syntax

result = string Like pattern

The Like operator syntax has these parts:

Part Description
result Required; any numeric variable.
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.
In Microsoft Windows the sort order is determined by the code page. In the following example, a typical sort order is shown:

0 < 9 <A < B < E < Z < a < b < e < 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 in string:

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.

To match the special characters question mark (?), number sign (#), and asterisk (*), enclose them in brackets. The left bracket ([) is also a special character, but only if it's followed by the right bracket (]). Otherwise it's treated as a regular character. Likewise, right bracket (]) in a group is always treated as a special character, but 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.

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 inside the brackets, the hyphen is used to identify a range of characters.

    When a range of characters is specified, they may appear in ascending or descending order. The range can be specified by ASCII symbols from number 48 to 122. The only exception is right bracket (]), which is number 93 in the ASCII table. For example, [A-Z], [4-1], [a-Z], [;-Z] are valid patterns. The expressions in brackets [*-4], [Z-.] [z-]] won't be considered as range, but will be treated as individual characters.
    Empty brackets [] are ignored, they are considered a zero-lenght string ("").

Example

Dim Result
Result = "aBBBa" Like "a*a" ' Returns True.
trace Result
Result = "F" Like "[A-Z]" ' Returns True.
trace Result
Result = "F" Like "[!A-Z]" ' Returns False.
trace Result
Result = "a2a" Like "a#a" ' Returns True.
trace Result
Result = "aM5b" Like "a[L-P]#[!c-e]" ' Returns True.
trace Result
Result = "BAT123khg" Like "B?T*" ' Returns True.
trace Result
Result = "CAT123khg" Like "B?T*" ' Returns False.
trace Result

 

See Also

Operators, Comparison Operators, InStr Function, StrComp Function