Como: Corresponder uma cadeia de caracteres a um padrão (Visual Basic)

If you want to find out if an expression of the Tipo de dados de sequência de caracteres (Visual Basic) satisfies a pattern, then you can use the Operador Like (Visual Basic).

Like takes two operands. O operando esquerdo é uma expressão de seqüência e o operando da direita é uma seqüência de caracteres que contém o padrão a ser usado para correspondência. LikeRetorna um Boolean valor que indica se satisfaz a expressão de seqüência de caracteres padrão.

You can match each character in the string expression against a specific character, a wildcard character, a character list, or a character range. The positions of the specifications in the pattern string correspond to the positions of the characters to be matched in the string expression.

To match a character in the string expression against a specific character

  • Put the specific character directly in the pattern string. Certain special characters must be enclosed in brackets ([ ]). For more information, see Operador Like (Visual Basic).

    The following example tests whether myString consists exactly of the single character H.

    Dim sMatch As Boolean = myString Like "H"
    

To match a character in the string expression against a wildcard character

  • Put a question mark (?) in the pattern string. Any valid character in this position makes a successful match.

    The following example tests whether myString consists of the single character W followed by exactly two characters of any values.

    Dim sMatch As Boolean = myString Like "W??"
    

To match a character in the string expression against a list of characters

  • Put brackets ([ ]) in the pattern string, and inside the brackets put the list of characters. Do not separate the characters with commas or any other separator. Any single character in the list makes a successful match.

    The following example tests whether myString consists of any valid character followed by exactly one of the characters A, C, or E.

    Dim sMatch As Boolean = myString Like "?[ACE]"
    

    Note that this match is case-sensitive.

To match a character in the string expression against a range of characters

  • Coloque entre colchetes ([ ]) na seqüência de caracteres padrão e dentro dos colchetes colocados os caracteres mais baixos e mais altos no intervalo, separados por um hífen (–). Any single character within the range makes a successful match.

    The following example tests whether myString consists of the characters num followed by exactly one of the characters i, j, k, l, m, or n.

    Dim sMatch As Boolean = myString Like "num[i-m]"
    

    Note that this match is case-sensitive.

Matching Empty Strings

Like treats the sequence [] as a zero-length string (""). You can use [] to test whether the entire string expression is empty, but you cannot use it to test if a particular position in the string expression is empty. If an empty position is one of the options you need to test for, you can use Like more than once.

To match a character in the string expression against a list of characters or no character

  1. Call the Like operator twice on the same string expression, and connect the two calls with either the Operador Or (Visual Basic) or the Operador OrElse (Visual Basic).

  2. In the pattern string for the first Like clause, include the character list, enclosed in brackets ([ ]).

  3. In the pattern string for the second Like clause, do not put any character at the position in question.

    O exemplo a seguir testa o número de telefone Dígito sete- phoneNum para exatamente três dígitos numéricos, seguidos por um espaço, um hífen (–), um período (.), ou nenhum caractere, seguido exatamente quatro dígitos numéricos.

    Dim sMatch As Boolean = 
      (phoneNum Like "###[ -.]####") OrElse (phoneNum Like "#######")
    

Consulte também

Referência

Operadores de comparação (Visual Basic)

Operador Like (Visual Basic)

Tipo de dados de sequência de caracteres (Visual Basic)

Conceitos

Operadores e expressões em Visual Basic