I have an application wrote by classic ASP. I need to compare if string1 contains string2 so I use instr function. However, even the string1 contains string2, it always returns 0. Where is the problem of my code?
function Findstring(string1,string2)
dim findstr,loc
on error resume next
loc= instr(1,Lcase(string1),Lcase(string2),1)
if loc>0 then
findstr=true
else
findstr= false
end if
end function
Function FindString( string1, string2 )
FindString = False
On Error Resume Next
FindString = CBool( InStr(1, LCase(string1), LCase(string2), 1) > 0 )
End Function
This function will return True
if string2
is contained inside string1
and False
in any other case.
note : Why the CBool
if the comparision operator >
already generates a boolean? Because we can call the function passing a Null
value. If any of the strings being compared is Null
, the InStr
function returns Null
and the test Null > 0
evaluates to Null
. But CBool( Null )
will generate an error, captured by the previous On Error
(that will also handle problems with objects)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.