PDA

View Full Version : INSTR



uktous
05-12-2013, 08:24 AM
INSTR function returns the position of the first occurrence of a substring in a string.

It starts from the left and stop at the right.

Is it possible to find the position of text A in text B from the right?

For example, I am a boy.
Search bo will return 2, instead of 8.

patel
05-12-2013, 09:39 AM
use instrrev

Aflatoon
05-13-2013, 12:55 AM
You could use StrReverse on both strings then use instr with the two reversed strings.

mancubus
05-13-2013, 01:54 AM
INSTR function returns the position of the first occurrence of a substring in a string.

It starts from the left and stop at the right.

Is it possible to find the position of text A in text B from the right?

For example, I am a boy.
Search bo will return 2, instead of 8.

both InStr (searches substring in string from left to right) and InStrRev (searches substring in string from right to left) functions return the same position number (from left to right) if the substring repeats only once in the string.



For example, I am a boy.
Search bo will return 2, instead of 8.

so you actually look for the position of "ob" rather than "bo"?

then take aflatoon's recommendation into account:

You could use StrReverse on both strings then use instr with the two reversed strings.

which may be:

Sub test()

txt = "I am a boy"

MsgBox InStr(StrReverse(txt), StrReverse("bo"))

End Sub