Like Operator


Like Operator

Syntax

     Dim result As Boolean = string Like pattern 


string (required; String)

The string to be tested against pattern


pattern (required; String)

A series of characters used as rules by which string is evaluated

Description

The Like operator indicates whether string matches pattern (true) or not (False). In general, characters match themselves; a pattern of "abc" matches a string of "abc." But there are a few special characters that enable generic pattern matching.

Character

Meaning

?

Matches any single character

*

Matches zero or more characters

#

Matches any single digit (09)


     [list] 

Matches any single character in list

     [!list] 

Matches any single character except those in list

The characters are used together to form patterns. For instance, the pattern "a?###" matches the letter a followed by any single character and then followed by three digits.

The list character in square brackets may include a simple set of characters, like "[3kd]," but it can also use the hyphen (-) character to specify a range, such as "[a-m0-9]," which specifies the lowercase letters a through m or the digits 0 through 9. Use a hyphen at the beginning or end of list to match the hyphen character.

To use special characters, such as #, as normal characters, enclose them in square brackets.

Usage at a Glance

  • If either string or pattern is Nothing, then result will be Nothing.

  • The default comparison method for the Like operator is binary (case sensitive). This can be overridden using the OptionCompare statement. The overall sort order is based on the code page currently being used, as determined by the Windows regional settings.

  • The exclamation point, when used outside of square brackets, matches itself.

  • Different written languages place different priorities on particular characters in relation to sort order. Therefore, the same program using the same data may yield different results when run on machines in different parts of the world, depending upon the locale settings of the systems.

  • Regular expressions provide an even more powerful method for searching and comparing strings. You can use regular expressions through the .NET Framework's System.Text.RegularExpressions.RegEx class.

Example

The following example matches a U.S. Social Security number, which is three groups of digits, 3, 2, and 4 digits, respectively. The groups are separated by hyphens.

     Public Function IsSSN(ByVal testText As String) As Boolean        If (testText Like "###-##-####") Then           Return True        Else           Return False        End if     End Function 




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net