|
|
|||||||||||||||
| H o m e | |||||||||||||||
|
|
I n t r o d u c t i o n t o t h e V B . N E T ‘ L i k e ’ O p e r a t o r By Michael McIntyre
The VB.NET ‘Like’ operator compares a string to a pattern returning True if the pattern is found in the string or False if the pattern is not found in the string.
Example One – Introducing the ‘Like’ Operator
In this first example a string containing the upper case F character is compared to a pattern containing the upper case F character.
Dim pattern1 As String = "F" Console.WriteLine(("F" Like pattern1).ToString)
RESULT: True
Patterns
Example one compared a string to a very simple pattern; a single upper case character.
A pattern can be any String expression conforming to the following pattern-matching conventions.
Example Two – Using ? in a pattern.
Dim pattern2 As String = "B?t" Console.WriteLine(("Bat" Like pattern2).ToString) Console.WriteLine(("Bit" Like pattern2).ToString)
RESULT:
True True
A question mark in a pattern matches a single character in a string. In example two the pattern “B?t” will return true when it is compared to any string containing an upper case B followed by any character following by a lower case t. Comparing the words Bit or Bat to the pattern B?t returns True.
Example Three – Using * in a pattern.
Dim pattern3 As String = "Jo*" Console.WriteLine(("John" Like pattern3).ToString) Console.WriteLine(("Joan" Like pattern3).ToString) Console.WriteLine(("JoAnn" Like pattern3).ToString)
RESULT:
True True True
An asterick in a pattern matches zero or more characters in a string. In example three the pattern “Jo*” will return true when is compared to a string containing an upper case J followed by a lower case 0 followed by 0 or more characters. Comparing the names John, Joan, and JoAnn to the pattern Jo* returns True.
Example Four – Using # in a pattern.
Dim pattern4 As String = "*3##" Console.WriteLine(("Part-301" Like pattern4).ToString) Console.WriteLine(("Part-321" Like pattern4).ToString)
RESULT:
True True
A pound sign in a pattern matches any single digit in a string. In example four the pattern “*3##” will return true when compared to a string containing 0 or more characters followed by a 3 followed by two digits. Comparing the part identifiers Part-301 and Part-321 to the pattern *3## returns True.
Example Five – Using [charlist] in a pattern.
Dim pattern5 As String = "B[a-f]t" Console.WriteLine(("Bat" Like pattern5).ToString) Console.WriteLine(("Bit" Like pattern5).ToString)
RESULT:
True False
A [charlist] in a pattern is a group of one or more characters (charlist) enclosed in brackets ([ ]) A [charlist] in a pattern can be used to match any single character in string and can include almost any character code, including digits. By using a hyphen (–) to separate the upper and lower bounds of the range, charlist can specify a range of characters. For example, [A-Z] results in a match if the corresponding character position in string contains any uppercase letters in the range A–Z.
In example five the pattern [a-f] will return true when compared to a string containing a character between lower case a and lower case f. Comparing the word Bat to the pattern returns true. Comparing the word Bit returns false because the character i does not fit the pattern.
For more information about the Like operator click here.
|
||||||||||||||
|
Copyright © 2001-2004 aZ Software Developers. All rights reserved. |
|||||||||||||||