2 Replies - 675 Views - Last Post: 05 October 2013 - 09:39 PM Rate Topic: -----

#1 Scorpio721  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 15-November 08

InStrRev returning InStr Value

Posted 05 October 2013 - 05:12 PM

Hey All,

I have a puzzler for today, I have these lines of code:
Dim s As String, ins As String, inp As Integer, inq As Integer
s = Me.Listbox.List(lp, 0)
inp = InStrRev(s, "\")
inq = InStr(s, "\")
ins = Right$(s, inp - 1)
MsgBox ("0: " & s & vbNewLine & 
      _"1: " & inp & vbNewLine & 
      _"2: " & inq & vbNewLine & 
      _"3: " & ins)


Now this is the output:
----------------------
0: \Picture_001.png
1: 1
2: 1
3:
----------------------

As you may gathered I am trying to read to the backslash from the end of the string and for this particular example get 16 but InStrRev returns 1...why?


Note: adapting line "ins = Right$(s, inp - 1)" without the "- 1" results in "3: g" (as you would expect if InStrRev returns 1, albeit incorrect)
Thanks in Advance

This post has been edited by Scorpio721: 05 October 2013 - 05:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: InStrRev returning InStr Value

#2 Scorpio721  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 15-November 08

Re: InStrRev returning InStr Value

Posted 05 October 2013 - 05:41 PM

Hmm, after a bit of googling it appears that InStrRev will return same position as InStr it just starts its search from the end rather than the start. You would think it would be more helpful to return the value as if counting from the end to the matching character.

Yep that solved it!
Simply get length of string (len) and then deducting the position value obtained via InStrRev gets me the correct result.
Right$(s, len(s) - inp)



Result: "Picture_001.png"

BTW: I am using this method of trimming the string, that you may have guessed is a path, as any number of characters can be before the "\" including another "\" so - this is the best way of achieving that IMO.

Hope this helps others in same situation or even avoid being in this situation.
Thanks

This post has been edited by Scorpio721: 05 October 2013 - 05:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: InStrRev returning InStr Value

Posted 05 October 2013 - 09:39 PM

I understand why you might think that that would be more helpful, but you need to think about characters in a string as you would a zero-based array. The returned value is the offset of the character that you are looking for from the starting memory location. The reason to have InStrRev, as you probably already understand, is because you want the last occurrence of a character or characters in a string rather than the first one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1