Link to home
Start Free TrialLog in
Avatar of graflunds
graflunds

asked on

vbscript like operator?

what's the equivalent to the like operator in vbscript??? I've got a long variable that would always have a certain text in it .. want to query if that text is in it like so ..

If (variable Like "*text*") Then

Else

End If

VBScript returns a runtime error that the sub or function isn't defined ..

Thanks
Avatar of AzraSound
AzraSound
Flag of United States of America image

hi,
take a look at regular expressions:
http://msdn.microsoft.com/workshop/languages/clinic/scripting051099.asp


this is for complex matching but other than using something simple like Instr as part of a workaround, i dont know of another way
ASKER CERTIFIED SOLUTION
Avatar of ssathya
ssathya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mberumen
mberumen

use the Instr() operator instead

if Instr(1,variable,"text") Then
Else
End if

regards
Returns a Variant (Long) specifying the position of the first occurrence of one string within another.

Syntax

InStr([start, ]string1, string2[, compare])

The InStr function syntax has these arguments:

Part      Description
start      Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start contains Null, an error occurs. The start argument is required if compare is specified.
string1      Required. String expression being searched.
string2      Required. String expression sought.
compare      Optional. Specifies the type of string comparison. The compare argument can be omitted, or it can be 0, 1or 2. Specify 0 (default) to perform a binary comparison. Specify 1 to perform a textual, noncase-sensitive comparison. For Microsoft Access only, specify 2 to perform a comparison based on information contained in your database. If compare is Null, an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison.
Return Values

If      InStr returns
string1 is zero-length      0
string1 is Null      Null
string2 is zero-length      start
string2 is Null      Null
string2 is not found      0
string2 is found within string1       Position at which match is found
start > string2      0
My suggestion will work in the case when you are looking for a string in another string, but you can use the objects suggested by the article referenced by AzraSound for more advanced wildcard searching.  If it is straightforward like the example, you can use Instr...
You want to look for whether the string is in the main string by using InStr i.e.:

Dim strMyString As String
Dim intStrAt As Integer
strMyString = "ABCDEFG"

intStrAt = InStr(1,strMyString, "text")
If intStrAt = 0 Then
 MsgBox "string not found"
else
 MsgBox "String found in strMyString"
End If

InStr returns an integer specifying where in the main string the requested string starts at. If the requested string - i.e. "text" - cannot be found, then InStr returns 0
Arrrrggghhhh!!!!!!!!
Avatar of graflunds

ASKER

that did it ... thanks ..

both of you guys had the same question at the same time ... but this one was first so I used it ..