Use the like Operator

Source: Internet
Author: User
Tags uppercase letter

Like: used to compare two strings.

LikeThe operator syntax includes the following parts:

Part Description
Result Required; any numeric variable.
String Required; any string expression.
Pattern Required. Any string expression follows the pattern matching conventions in the description.

IfStringAndPattern
Match, thenResultIsTrue;If notResultIs
False. However, ifStringOr
PatternIf one of them is nullResultIsNull

The built-in pattern matching function provides multiple tools for string comparison. With the pattern matching function, you can use wildcards, string lists, or any combination of character ranges to match strings. The following table indicatesPatternAnd what they match:

Character in Pattern YesIn string
? Any single character.
* Zero or multiple characters.
# Any number (0-9 ).
[Charlist] CharlistAny single character in.
[!Charlist] NoCharlistAny single character in.

In brackets ([]Can contain one or more characters (Charlist)Groups andStringMatch any character in. This group contains almost any character code and number.

Note:To ([), Question mark (?), Numeric symbols (#) And the star number (*. You cannot use parentheses (].

By using a hyphen (-),CharlistYou can specify the character range. For example, ifStringContains any uppercase letter between A-Z[A-Z]Get a match. There is no need for delimiters. Square brackets can contain multiple ranges.


The following lists other important rules for pattern matching:

  • CharlistExclamation point at the beginning(!) Means that ifStringAnyCharlist. If an exclamation point is used outside square brackets, the exclamation point matches itself.

  • Hyphen (-) Can appear inCharlist(If an exclamation point is used, after the exclamation point ),Charlist. In any other place, a hyphen is used to identify the character range.
  • When the character range is specified, these characters must be displayed in ascending order (from minimum to maximum.[A-Z]Is a valid mode,[Z-A]No.
  • Character Sequence[]It is considered as a string ("") with zero length ("").

This example usesLikeOperator to compare strings.

This example uses the like operator to compare strings. Dim mycheckmycheck = "abbba" like "A * A" 'returns true. Mycheck = "F" like "[A-Z]" 'returns true. Mycheck = "F" like "[! A-Z] "'returns false. Mycheck = "A2a" like "A # A" 'returns true. Mycheck = "am5b" like "A [L-P] # [! C-E] "'returns true. Mycheck = "bat123khg" like "B? T * "'returns true. Mycheck = "cat123khg" like "B? T * "'returns false.
You can use the like operator to easily restrict characters in a text box, for example, only numbers and letters are allowed:
Private Sub Text1_Change()Dim s As StringFor i = 1 To Len(Text1)s = Mid(Text1, i, 1)If s Like "[!0-9]" And s Like "[!A-Z]" And s Like "[!a-z]" ThenText1 = Replace(Text1, s, "")End IfNextEnd Sub

Note: The materials are taken from msdn. I feel very useful when I limit the scope today. I will review them again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.