Reputation: 1124
I don't understand the results of any of the below. I understand InStrRev gives position of last 'matches' (right-to-left) & can comprehend the result when I don't give a start position. No matter if the start begins at left or right, 1 or 2 should give something other than 0.
Private Sub Form_Load()
Dim TestString As String
TestString = "ABCDEFGHCIJK"
msgbox = InStrRev(TestString, "C", 1) 'produces 0 if optional start parameter is 1 or 2
msgbox = InStrRev(TestString, "C", 3) 'produces 3 if start parameter is 3-8
msgbox = InStrRev(TestString, "C", 9) 'produces 9 if start parameter is 9-12
'13+ produces 0 as expected since its length of string is only 12
End Sub
Microsoft documentation on function: access & visual studio. On the visual studio page I tried the example at the bottom of the page & got same result, so I know its working. However my brain isn't getting. I also understand the InStr function with & without a start position
Upvotes: 2
Views: 2624
Reputation: 1124
OMG I just got it! I am terrible, I have a tendency to over-think things. The below is what I think I got hung up on & some documentation didn't explicitly clarify:
I couldn't have figured this out without HansUp. Hopefully the below is another way to look at it in case anyone else has the same issue understanding:
The below returns 5. Before A, the position is 1, before B 2 & so on. We told it to start at before C & search remainder of string (CDEFGHIJKLMN) which did have E in it still
InStr(3, "ABCDEFGHIJKLMN", "E")
The below returns 0. We told it to start at before C & search remainder of string (AB) which did not have E in it. The reason the remainder of the string was on AB was because it searches from RIGHT-TO-LEFT
InStrRev("ABCDEFGHIJKLMN", "E", 3)
Upvotes: 2
Reputation: 97131
I'll offer a different example which I hope makes the situation clearer. In the Immediate window (go there with the Ctrl+g shortcut) ...
TestString = "123456789"
? InStrRev(TestString, "3", 2)
0
That statement asked InStrRev
to start from the second character in TestString
and search backwards for the character "3". Since the first two characters of TestString
are "12", "3" is not found in that substring, so InStrRev
returns 0.
In this example, I ask InStrRev
to start searching from the fourth character ...
? InStrRev(TestString, "3", 4)
3
Since the first four characters of TestString
are "1234", "3" is found as the third character, so InStrRev
returns 3.
Regarding the statement "1 or 2 should give something other than 0" ... that is not true, as my first example demonstrates.
Upvotes: 1