Like Operator

   
Like Operator

Syntax

   result   =   string   Like   pattern   
string (required; String)

The string to be tested against pattern

pattern (required; String)

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

Return Type

Boolean

Description

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

Rules 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. 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 a 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 is used to match 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] will match 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 point in pattern matching is like the negation operator in C. Use an exclamation point 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 point outside of the bracket matches itself.

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

Example

The following example will display 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.NET Language in a Nutshell" returns OK .

 Private Sub Button1_Click(ByVal sender As System.Object, _                           ByVal e As System.EventArgs) _             Handles Button1.Click    Dim sTitle As String = "in a Nutshell"    Dim sPattern As String = "[V A]* " & sTitle    If TextBox1.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.

  • 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.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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