Like Operator

The Like operator is used to determine if a string matches a given pattern, returning True if the string does match the pattern, and False if it does not. The syntax is:

[string] Like [pattern]

The following statement will return True: Debug.Write ("F' Like "F") 'return True

The following statements will return False: Debug.Write ("FF" Like "F") 'return False Debug.Write ("F" Like "FF") 'return False

The following statement will return True if Option Compare is Text, and False if Option Compare is Binary:

Debug.Write ("F" Like T) True if Option Compare Text, otherwise False ln the previous examples, the Like operator operates no differently than the equality operator. The real power of the Like operator is using pattern matching.

Pattern matching is often used in everyday computing activities. For example, in searching for a file on your computer that you know starts with "msado" and has the extension .dll, you could do a search for the file msado*.dll, using the wildcard character

Table 6-3 lists the pattern matching characters supported by Visual Basic .NET.

Table 6-3: Characters for Pattern Matching

Characters in Pattern

Matches in String

?

Any single character

*

Zero or more characters

#

Any s ingle digit (0-9)

[charlist]

Any single character in charlist

[!charlist]

Any single character not in charlist

The wildcard character * is commonly used for searches, particularly when not all of the details of the string being searched for are known or remembered. The following statement returns True because "aBBBa" has an "a" at the beginning, an "a" at the end, and any number of characters in between:

"aBBBa" Like "a*a"

The wildcard character ? provides for a more focused search than * because, while the * wildcard character can represent zero or more characters, the ? wildcard character represents one character, no more and no less. The following statement returns True because "BAT" starts with a "B," ends with a "T," and has exactly one character in between:

The wildcard characters ? and *, like others, can be combined. The following statement returns True because "BAT" starts with a "B," followed by any single character, followed by a "T," and finally zero or more characters of any type.

"BAT123khg" Like "B?T*"

The wildcard character # provides for an even more focused search than the wildcard character ? because, while the ? wildcard character can represent any character, the # wildcard character can only represent a character that signifies a digit. The following statement returns True because "a2a" begins and ends with an "a" and has exactly a single digit number in between: "a2a" Like "a#a"

A group of one or more characters (charlist) enclosed in brackets ([ ]) can be used to match any single character in a string and can include almost any character code, even digits. The following statement returns True because "F" occurs in the set of characters from A to Z:

Note When a range of characters is specified, they must appear in ascending sort order, from lowest to highest. Thus, [A-Z] is a valid pattern, but [Z-A] is not.

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 th e string. The following statement returns False because "F" occurs in the set of characters from A to Z:

Comparison operators rank lower than the arithmetic operators discussed in Module 5 and higher than the logical operators discussed in the next section, "Logical Operators." All comparison operators are of equal precedence, and are evaluated from left to right.

Was this article helpful?

0 0

Post a comment