Showing posts with label Instr. Show all posts
Showing posts with label Instr. Show all posts

Tuesday, 13 May 2014

QTP-UFT-Can you check if a value is present in a string or not

Yes , We can use Instr method

Example:-
ReturnValue= Instr(1,"UFTQuestions","Questions")
Return 0 if the string is not found other wise returns the starting Position at which match is found

In our case

ReturnValue will return 4

To check it run the below code in QTP/UFT

Syntax
ReturnValue= Instr(1,"UFTQuestions","Questions")
Msgbox ReturnValue


Friday, 2 May 2014

QTP - UFT - What does instr function return after execution or Instr Function




Return 0 if the string is not found other wise returns the starting Position at which match is found

' Syntax
' 'instr(start,String,string2,comparetype)

Here is an example

' Code: 
 strFullString = "Saturday and sunday are holidays"
 strlookforString = "day"
 returnval = instr(1,strFullString,strlookforString)
 msgbox returnval

Result is 6
Since "day" has occurred at 6 position instr function returns 6


' Code: 
 strFullString = "Saturday and sunday are holidays"
 strlookforString = "day"
 returnval = instr(7,strFullString,strlookforString)
 msgbox returnval

Result is 7
Since "day" has occurred at 17 position since we have started to check from position 7 in the strFullString so, instr function returns 17.

Note :- As shown above if multiple strings exists with the same name it will return the first occurrence of the search string by default


Note:- the 4th Parameter (comparetype) is not a mandatory and user can ignore if they are want

vbBinaryCompare - This performs binary comparison.
vbTextCompare - This performs text comparison

Note :- By default binary comparison is performed

So what happens when we perform Binary Comparison
' Code: 
 strFullString = "Saturday and Sunday are holidays"
 strlookforString = "Day"
 returnval = instr(7,strFullString,strlookforString)
 msgbox returnval

Result is 0 since we have used CAP "D" in strlookforString.

Happy reading,
Sreenu Babu


Sunday, 27 April 2014

UFT - QTP - How to get certain part of string using VBScript




 

 
 

We Can use the mid method
'output = Mid(InputString, StartPosition, NoofCharacterRequired)

Syntax
output = Mid("UFT is Great Fun", 5, 5)
Msgbox output

Alternate method

' Comments
'Extract a substring between a "Pre" substring and an "Post" substring.
'e.g.  Util_Text_SubStr("This is my string", "is ", "r")
' will return "is my st"

Function  Util_Text_SubStr(ByVal sString, ByVal sPre, ByVal sPost)
    Dim nStart, nEnd
    Util_Text_SubStr = ""
    If VarType(sString) <> vbString Then
        Exit Function
    End If
    nStart = instr(1,sString,sPre,vbTextCompare)
    If nStart = 0 Then
        Exit Function
    End If
    nStart = nStart + Len(sPre)
    If nStart > Len(sString) Then
        Exit Function
    End If
    If Len(sPost)>0 Then
        nEnd = instr(nStart,sString,sPost,vbTextCompare)
        If nEnd = 0 Then
            Exit Function
        End If
        nEnd = nEnd-1
    Else
        nEnd = Len(sString)
    End If
    If nEnd > nStart Then
        Util_Text_SubStr = mid(sString,nStart,(nEnd-nStart)+1)
    End If
End Function

Happy reading,
Sreenu Babu