InStr Function


InStr function searches for the substring in main string and returns the position of first occurrence of substring within main string.

Returns 0 in case no occurrence.

Syntax: InStr([start],String1,String2,[Compare])

Start (Optional) – Specifies the starting position of the search. If not specified, the search begins from the first position of the string

String1 (Required) – Main string

String2 (Required) – Sub string to search in Main string

Compare (Optional) – Specifies the type of comparison to be performed. It takes the below two values –
          0 (VbBinaryCompare) – Performs Binary comparison
          1 (VbTextCompare) – Performs Text comparison

If not specified, it performs Binary comparison by default.

Difference between Binary and Text Comparisons:

Let us assume that we are comparing A with a.

Binary Comparison compares byte to byte. So, A (ASCII 65) is different than a (ASCII 92). So, in case of Binary comparison A does not match with a.

Text Comparison compares the text. So, A would be same as a. So, in case of Text comparison A matches with a.

Examples:

InStr(1,"QTP Sreenu","S") – Output is 5
InStr("QTP Sreenu","S") – Output is 5'Starting position is not mentioned
InStr(1,"QTP Sreenu","s",0) – Output is0'Binary Comparison - Main string does not have lower case s
InStr(1,"QTP Sreenu","s",1) – Output is5'Text Comparison – Lower case s matches with upper case S
InStr(5,"InStr Function Sample","S") - Output is16'Search starts from position 5. So, first S would be skipped

No comments:

Post a Comment