简体   繁体   中英

Comparison help through Instr Function

I want to convert int into string in VB6 and parse that string into Instr function, but I am unable to do that, here is my code:

'.WebElement("total_Count").GetROProperty("innertext")=1 - 10 de 31 candidatos 
'This is the value in innertext, and i want to compare the 31

totalCount=31
CStr (totalCount)
        If InStr(totalCount,.WebElement("total_Count").GetROProperty("innertext"))>0Then
                MsgBox "Found"
                Reporter.ReportEvent micPass,"DBVerification","TotalCount Verified From DB"
                Else
                MsgBox "Not Found"
                Reporter.ReportEvent micFail,"DBVerification","TotalCount Not Verified From DB"
        End If

Thanks for your help

Comparisons on VB6 are case sensitive unless Option Compare Text is used at top of the module/form/class; in this particular case it wouldn't matter though. Also, CStr(totalCount) is not assigning to anything is not changing TotalCount into a string; it returns a string.

totalCount=31

If InStr(lcase(CStr(totalCount)),lcase(.WebElement("total_Count").GetROProperty("innertext")))>0 Then
   MsgBox "Found"
   Reporter.ReportEvent micPass,"DBVerification","TotalCount Verified From DB"
Else
   MsgBox "Not Found"
   Reporter.ReportEvent micFail,"DBVerification","TotalCount Not Verified From DB"
End If

为什么不将它们与数字进行比较,即您的处理方式,如果totalcount = 31并且innertext包含1,您将得到True,这肯定不是您想要的。

Swap the arguments of the Instr() function:

InStr(.WebElement("total_Count").GetROProperty("innertext"), totalCount)>0

Now you are looking if 31 is inside your innertext. You don't have to use cStr() , lCase() or other stuff, VBScript will do that for you.

Edit: What you really want to make it work right is a regular expression of course:

Dim regEx, matches, actualTotal
set regEx = new RegExp
regEx.Global = True
regEx.pattern = "\d+ - \d+ de (\d+) candidatos"
Set matches = regEx.Execute(Value)
actualTotal = matches(0).submatches(0)

Now you can compare actualTotal with the expected total, without the bothering if the number is in the rest of the string. For example: "31 - 40 de 42 candidatos" will result in a false positive. Using a RegExp you prevent that behaviour.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM