Share via


Like Operator (Visual Basic)

Compares a string against a pattern.

result = string Like pattern

Parts

  • result
    Required. Any Boolean variable. The result is a Boolean value indicating whether or not the string satisfies the pattern.

  • string
    Required. Any String expression.

  • pattern
    Required. Any String expression conforming to the pattern-matching conventions described in "Remarks."

Remarks

If the value in string satisfies the pattern contained in pattern, result is True. If the string does not satisfy the pattern, result is False. If both string and pattern are empty strings, the result is True.

Comparison Method

The behavior of the Like operator depends on the Option Compare Statement. The 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. 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. 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

Character Lists

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.

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.

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. 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 (Visual Basic).

Character Ranges

By using a hyphen (–) to separate the lower and upper 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 character within the range A–Z, and [!H–L] results in a match if the corresponding character position contains any character outside the range H–L.

When you specify a range of characters, they must appear in ascending sort order, that is, from lowest to highest. 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. For example, [A–CX–Z] results in a match if the corresponding character position in string contains any character within either the range A–C or the range X–Z.

Usage of the Hyphen

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

The meaning of a specified range depends on the character ordering at run time, as determined by OptionCompare and the locale setting of the system the code is running on. With OptionCompareBinary, the range [A–E] matches A, B, C, D, and E. With OptionCompareText, [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. 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 digraph character and the two individual characters are equivalent.

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. Similarly, 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

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. For more information, see Operator Procedures (Visual Basic).

Example

This example uses the Like operator to compare strings to various patterns. 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

Tasks

How to: Match a String against a Pattern (Visual Basic)

Reference

Comparison Operators (Visual Basic)

InStr

Operator Precedence in Visual Basic

Operators Listed by Functionality (Visual Basic)

Option Compare Statement

StrComp

Concepts

Operators and Expressions in Visual Basic