Like Operator

Syntax

result = string Like pattern


result

Use: Required

Data Type: Boolean

If string matches pattern, result is True; otherwise, result is False.


string

Use: Required

Data Type: String

The string to be tested against pattern.


pattern

Use: Required

Data Type: String

A series of characters used by the Like operator to determine if string and pattern match.

Description

Determines if a string matches a given pattern.

Rules at a Glance

  • If either string or pattern is Null, then result is Null.

  • The default comparison method for the Like operator is binary. This can be overridden using the Option Compare statement.

  • Binary comparison is based on comparing the internal binary number representing each character; this produces a case-sensitive comparison.

  • Text comparison, the alternative to binary comparison, is case insensitive; therefore, A = a.

  • The sort order is based on the code page currently being used, as determined by the Windows regional settings.

  • The following table describes the special characters to use when creating pattern; all other characters match themselves:

    Character Meaning
    ? Any single character
    * Zero or more characters
    # Any single digit (0-9)
    [list] Any single character in list
    [!list] Any single character not in list
    [] A zero-length string ("")

  • list matches a group of characters in pattern to a single character in string and can contain almost all available characters, including digits.

  • Use a hyphen (-) in list to create a range of characters to match a character in string ; for example [A-D] matches A,B,C, or D in that character position in string.

  • Multiple ranges of characters can be included in list without the use of a delimiter. For example, [A-D J-L].

  • Ranges of characters should appear in sort order; for example, [c-k].

  • Use the hyphen at the start or end of list to match to itself. For example, [- A-G] matches a hyphen or any character from A to G.

  • The exclamation mark in pattern matching is similar to the negation operator in C. Use an exclamation mark before a character or range of characters in list to match all but that character. For example, [!A-G] matches all characters apart from the characters from A to G.

  • The exclamation mark outside of the bracket matches itself.

  • To use any special character as a matching character, enclose the special character in brackets. For example, to match to a question mark, use [?].

Example

The following example displays OK if the text entered into Text1 starts with either V or A, followed by any characters and ends with "in a Nutshell." Therefore "Paul in a Nutshell" returns Wrong, whereas either "ASP in a Nutshell" or "VB & VBA in a Nutshell" is OK.

Private Sub Command1_Click()
    Dim sTitle As String, sPattern As String
    sTitle = "in a Nutshell"
    sPattern = "[V A]* " & sTitle
    If Text1.Text Like sPattern Then
        MsgBox "OK"
    Else
        MsgBox "Wrong"
    End If    
End Sub

Programming Tips and Gotchas

Different languages place different priority on particular characters with 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.

See Also

Option Compare Statement
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.94.79