Like operator (LotusScript® Language)

Determines whether a string expression matches a pattern string.

Syntax

stringExpr Like patternString

Elements

stringExpr

Any String expression.

patternString

A string expression that can include any individual ANSI characters and any of the wildcard characters or collections that follow. You can use as many wildcards as you need within a single patternString.

Wildcard

Matches

?

Any one character

#

Any one digit from 0 through 9

*

Any string (zero or more characters)

[ characters ]

Any one of the characters in the list or range specified here

[! characters ]

Any one character not included in the list or range of characters specified here

Matching characters in a list

To match characters in a list, enclose the characters between brackets with no spaces or other delimiters between characters (unless you want the space character to be part of the list). For example, [1, 2, 3, A, B, C] represents the characters 1, comma, space, 2, 3, A, B, and C (the redundant occurrences of the space and comma are ignored). But [123ABC] represents the characters 1, 2, 3, A, B, and C (with no space or comma character).

Matching characters in a range

To match characters in a range, separate the lower and upper bounds with a hyphen, as in [1-5]. Always specify the range in ascending sort order (A-Z rather than Z-A). Sort order is determined by the setting of Option Compare. When you specify multiple ranges, you don't have to separate them with anything: for example, [1-5A-C] contains the ranges 1-5 and uppercase A-C.

If binary comparison (Option Compare Binary) is in effect, LotusScript® uses the international Unicode character collating sequence. This is the sequence of values Uchr(0), Uchr(1), Uchr(2), .... It is the same on all LotusScript® platforms. A range specified in ascending order will produce a valid pattern string. However, if Option Compare Case, NoCase, Pitch, or NoPitch is in effect, then the collating sequence order depends to some extent on the operating system that you are using. It is usually better in any case to avoid ranges for punctuation and other ambiguous characters. For better maintainability and platform-independence, specify the characters you want to match against (for example, "[:;<=>?]") instead of using a range. Also note that you can define ranges by exclusion; to match non-alphanumeric characters, use "[!a-Z0-9]" (in non-binary mode) or "[!A-Za-z0-9]" (in binary mode), instead of trying to list all the non-alphanumeric Unicode characters.

Matching special characters

To match one of these characters, include it in a characters list:

  • Hyphen ( - )
  • Question mark ( ? )
  • Asterisk ( * )
  • Number sign ( # )
  • Open bracket ( [ )

Be sure to place the hyphen at the beginning of the list; if you're using the [!characters] format, the hyphen immediately follows the exclamation point, as in [!-123]. The other characters can appear anywhere in the characters list. For example, with Option Compare Binary (and therefore the Unicode collation sequence) in effect, [-?A-Z] matches the hyphen, the question mark, and any uppercase letter from A through Z.

To match one of these characters, place the character anywhere within your wildcard specification except in a characters list or range:

  • Comma ( , )
  • Close bracket ( ] )
  • Exclamation mark ( ! )

For example, !,[1-6] matches the exclamation point, the comma, and any digit from 1 through 6.

Return value

If stringExpr matches patternString, the result is TRUE; if not, the result is FALSE. If either stringExpr or patternString is NULL, the result is NULL.

Usage

By default, the comparison is case sensitive. You can modify case sensitivity with the Option Compare statement.

Examples

Example 1

This example prints the two-digit numbers from 1 to 100 that end in 3 and don't begin with 2.

For x = 1 To 100
   If CStr(x) Like "[!2]3" Then Print x
Next x
' Output:
' 13 33 43 53 63 73 83 93 

Example 2

This example uses Like as a validation formula for city and zip fields.

if doc.city(0) like "*[0-9]*" then messagebox _
   "city field contains a number"
if doc.zip(0) like "*[a-z,A-Z]*" then messagebox _   
   "zip code field contains a character"

Example 3

This example shows some ways you can test a string with Like to see if it contains a given substring:

' Make string comparison case-sensitive.
Option Compare Case
Dim anArray(1 To 6) As String
anArray(1) = "Juan"
anArray(2) = "Joan"
anArray(3) = "Alejandro"
anArray(4) = "Jonathan"
anArray(5) = "Andrea"
anArray(6) = "Jane"
UB% = UBound(anArray)

' Test each name in anArray$ to see if it contains a substring
' consisting of any characters followed by uppercase J
' followed by any characters followed by lowercase n followed
' by any characters.
For counter% = 1 to UB%
  If anArray(counter%) Like "*J*n*" Then
      Print anArray(counter%) & " " ;
  End If
Next
Print ""
' Output: Juan Joan Jonathan Jane

' Test each name in anArray$ to see if it contains
' a numeric character.
badRec% = 0
For counter% = 1 to UB%
  If anArray(counter%) Like "*#*" Then
      Print anArray(counter%) & " contains a numeral."
      badRec% = badRec% + 1
  End If
Next
If badRec% = 0 Then
  Print "No name contains a numeral."
End If
' Output: No name contains a numeral.

' Test the lowercase representation of each name in anArray$
' to see if it ends in a vowel.
For counter% = 1 to UB%
  If anArray(counter%) Like "*[aeiou]" Then
      Print anArray(counter%) & " " ;
  End If
Next
Print ""
' Output: Alejandro Andrea Jane