For the like operator, see

Source: Internet
Author: User
Tags ranges uppercase letter

Like Operator

Comparison operator | instr function | Operator priority in Visual Basic | Operator (listed by function) | option compare statement | strcomp function | Operator
Compares two strings.

Result = string like pattern
Description
Result
Required. Any Boolean variable. The result is a Boolean value, indicating whether the string matches pattern.
String
Required. Any string expression.
Pattern
Required. Any string expression that meets the pattern matching conventions described in the remarks section.
Remarks
If the string matches pattern, the result is true. If the string does not match, the result is false. If both string and pattern are empty strings, the result is true. Otherwise, if either string or pattern contains an empty string, the result is false.

The behavior of the like operator depends on the option compare statement. The default string comparison method for each module is option compare binary.

The built-in pattern matching provides a versatile tool for string comparison. The pattern matching function allows you to use wildcards, character lists, or any combination of character ranges to match strings. The following table shows the matching items of the characters allowed in pattern and those characters:

Match in string in Pattern
? Any single character
* Zero or more characters
# Any single number (0 to 9)
[Charlist] any single character in charlist
[! Charlist] any single character not in charlist

One or more character groups (charlist) enclosed in square brackets ([]) can be used to match any single character in a string, and can contain almost any character code (including numbers ).

Note that to match special characters, such as left square brackets ([) and question marks (?) , Number (#) and Star (*), enclose them in square brackets. You cannot use the right square brackets (]) in the group to match itself, but you can use it as a single character outside the group.
Use hyphens (-) to separate upper and lower limits of a range. charlist can specify the range of characters. For example, if the corresponding character location in a string contains any uppercase letter in the range A-Z, a match is produced for [A-Z. You can include multiple ranges in square brackets without separators.

The meaning of the specified range depends on the collation of valid characters at run time (determined by option compare and the region of the system that runs the Code ). When option compare binary is used, the range [A-E] matches A, B, C, D, and E. When option compare text is used, the [A-E] matches a, a, à, à, B, B, C, C, D, D, E, and E. This range does not match the occurrence or occurrence, because in the sorting order, the accent character is placed after the non-accent character.

Other important rules for pattern matching include:

Exclamation point at the beginning of charlist (!) It means that the match only occurs when any character except charlist is found in the string. When used outside square brackets, the exclamation point matches itself.
A hyphen (-) can appear at the beginning of the charlist (if there is an exclamation point, it will be behind it) or at the end of the character match itself. At any other position, a hyphen identifies the character range defined by the characters on both sides of the hyphen.
When specifying the character range, these characters must appear in ascending order (from the lowest to the highest ). Therefore, [A-Z] is a valid pattern, but [Z-A] is not.
The Character Sequence [] is considered as a zero-length string ("").
In some languages, there are some special characters in the alphabet that represent two different characters. For example, how many languages use characters? To indicate characters A and E (when these two characters appear together ). The like operator assumes that a single special character is equivalent to the two characters.

When a special character language is specified in the system region settings, a single special character in pattern or string matches the equivalent double Character Sequence in other strings. Similarly, a single special character (exists independently, in the list, or within a certain range) in the pattern enclosed in square brackets matches the string's equivalent double character sequence.

Example
In this example, the like operator is used to compare the string with the pattern. The result is a Boolean value, indicating whether the string meets the pattern.

Dim mycheck as Boolean
Mycheck = "F" like "F" 'Does "F" match "F "? Returns true.
Mycheck = "F" like "F" 'Does "F" match "F "? Returns false.
Mycheck = "F" like "fff" 'Does "F" match "fff "? Returns false.
Mycheck = "abbba" like "A * A" 'Does "abbba" have a "A" at
'Ning inning, an "A" at the end, and any number of characters in
'Between? Returns true.
Mycheck = "F" like "[A-Z]" 'Does "F" occur in the set
'Characters from A to Z? Returns true.
Mycheck = "F" like "[! A-Z] "'Does" F "not occur in the set
'Characters from A to Z? Returns false.
Mycheck = "A2a" like "A # A" 'Does "A2a" begin and end with
'"A" and have any single-digit number inbetween? Returns true.
Mycheck = "am5b" like "A [L-P] # [! C-E] "'Does" am5b "fit the following
'Pattern': begins with "A", has and character from the set L through
'P, followed Byb any single-digit number, and finally contains any
'Character excluded from the character set C through E. returns true.
Mycheck = "bat123khg" like "B? T * "'Does" bat123khg "fit
'Following pattern: begins with "B", followed by any single
'Character, followed by a "T" and finally zero or more characters
'Of any type. returns true.
Mycheck = "cat123khg" like "B? T * "'Does" cat123khg "fit
'Following pattern: begins with "B", followed by any single
'Character, followed by a "T" and finally zero or more characters
'Of any type. returns false.

 

 

For the option compare statement, see
Comparison operator | instr function | rev function | option strict statement | option explicit statement | replace function | split function | strcomp function |/optionexplicit |/optionstrict |/optioncompare: Binary |/optioncompare: text
The default comparison method used to declare string data at the file level.

Option compare {binary | text}
Description
Binary
Optional. This results in string comparison based on the sort order exported from the character's internal binary representation.
Text
Optional. This results in string comparison based on the case-insensitive text sorting sequence determined by the system region settings.
Remarks
If used, the option compare statement must appear before any other source statement in the file.

The option compare statement specifies a string comparison method (binary or text) for a class, module, or structure ). If the option compare statement is not included, the default text comparison method is binary.

In Microsoft Windows, the sorting order is determined by the Code Page. In the following example, you can use option compare binary to sort the characters in a typical binary order:

A <B <e <z <à <? <À <strong <?
If you use option compare text to sort the same characters, the following text sorting order is generated:

(A = A) <(à = à) <(B = B) <(E = e) <(Clerk = Clerk) <(Z = z) <(? = ?)
Example
This example uses the option compare statement to set the default string comparison method. You can only use the option compare statement at the module level.

'Set the string comparison method to binary.
Option compare binary 'That is, "AAA" is less than "AAA ".
'Set the string comparison method to text.
Option compare text 'That is, "AAA" is equal to "AAA ".
See
Comparison operator | instr function | rev function | option strict statement | option explicit statement | replace function | split function | strcomp function |/optionexplicit |/optionstrict |/optioncompare: Binary |/optioncompare: text

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.