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


No comments:

Post a Comment