This documentation is archived and is not being maintained.

11.15 Like Operator

Visual Studio .NET 2003

The Like operator is defined for the String type and determines whether a string matches a given pattern. The first operand is the string being matched, and the second operand is the pattern to match against. The pattern is made up of Unicode characters. The following character sequences have special meanings:

  • The character ? matches any single character.
  • The character * matches zero or more characters.
  • The character # matches any single digit (0–9).
  • A list of characters surrounded by brackets ([ab...]) matches any single character in the list.
  • A list of characters surrounded by brackets and prefixed by an exclamation point ([!ab...]) matches any single character not in the character list.

Two characters in a character list separated by a hyphen (-) specify a range of Unicode characters starting with the first character and ending with the second character. If the second character is not later in the sort order than the first character, a run-time exception occurs. A hyphen that appears at the beginning or end of a character list specifies itself.

Note   To match the special characters left bracket ([), question mark (?), number sign (#), and asterisk (*), brackets must enclose them. The right bracket (]) can not 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 to be the string literal "".

Also note that character comparisons and ordering for character lists are dependent on the type of comparisons being used. If binary comparisons are being used, character comparisons and ordering are based on the numeric Unicode values. If text comparisons are being used, character comparisons and ordering are based on the current locale being used on the .NET Framework.

In some languages, special characters in the alphabet represent two separate characters and vice versa. For example, several languages use the character æ to represent the characters a and e when they appear together, while the characters ˆ and O can be used to represent the character Ô. When using text comparisons, the Like operator recognizes such cultural equivalences. In that case, 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 the string and vice versa.

LikeOperatorExpression ::= Expression Like Expression

See Also

11.12.1 Operator Precedence and Associativity | 11.12.2 Object Operands | 11.12.3 Operator Resolution | 11.13 Arithmetic Operators | 11.14 Relational Operators | 11.16 Concatenation Operator | 11.17 Logical Operators | Like Operator (Visual Basic Language Reference)

Show: