QTP: check if array of strings contains value

I am having problems with the correct operation of my test case.

The problem is the code below, with the if if statement to be accurate. QTP complains that an object is required

For j=Lbound(options) to Ubound(options)
    If options(j).Contains(choice) Then
        MsgBox("Found " & FindThisString & " at index " & _
        options.IndexOf(choice))
    Else
        MsgBox "String not found!"
    End If
Next

When I check the array, I see that it is filled correctly, and "j" is also the correct string. Any help in this matter would be greatly appreciated.

+6
source share
5 answers

Strings in VBScript are not objects because they do not have member functions. Substring searches must be performed using a function InStr.

For j=Lbound(options) to Ubound(options)
    If InStr(options(j), choice) <> 0 Then
        MsgBox("Found " & choice & " at index " & j
    Else
        MsgBox "String not found!"
    End If
Next
+15
source

, , Filter UBound:

        If Ubound(Filter(options, choice)) > -1 Then
           MsgBox "Found"
        Else
           MsgBox "Not found!"
        End If

: ,

: , , .

+13
Function CompareStrings ( arrayItems , choice )
For i=Lbound(arrayItems) to Ubound(arrayItems)

' 0 - for binary comparison "Case sensitive
' 1 - for text compare not case sensitive
If StrComp(arrayItems(i), choice , 0) = 0 Then

MsgBox("Found " & choice & " at index " & i

Else

MsgBox "String not found!"

End If

Next

End Function
0
source

@gbonetti - your code returns "Found" for the following example. I think this is due to the reason mentioned by @Giacomo Lacava.

    a = Array("18")

    If Ubound(Filter(a, "1")) > -1 Then
       MsgBox "Found"
    Else
       MsgBox "Not found!"
    End If
0
source

Hi, if you check the exact string String not sub-String in the array, use StrComb, because if you use InStr, then if array = "apple1", "apple2", "apple3", "apple" and choice = "apple", then everyone will return pass for each element of the array.

Function CompareStrings ( arrayItems , choice )

For i=Lbound(arrayItems) to Ubound(arrayItems)

    ' 1 - for binary comparison "Case sensitive
    ' 0 - not case sensitive
    If StrComp(arrayItems(i), choice , 1) = 0 Then

    CompareStrings = True
    MsgBox("Found " & choice & " at index " & i

    Else

    CompareStrings = False
    MsgBox "String not found!"

    End If

Next

End Function
-2
source

All Articles