Login   /   Register

Find last occurrence of sub string within a string?

Rate this article
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...
April 1st, 2008 by daniva

Find the starting position of the last occurrence of a sub string within a string. ( With strings, the first character is at position 1 )

Dim s 
s = "QuickTest Professional quicktest professional"
 
' ** The following 4 lines are case-sensitive
Print InStrRev( s, "Professional" )     ' Prints 11
Print InStrRev( s, "QuickTest" )        ' Prints 1
Print InStrRev( s, "QUICKTEST" )        ' Not found, Prints 0
Print InStrRev( s, "professional" )     ' Prints 34
 
' ** The following 3 lines are case-insensitive
' ** The 3rd argument is the starting position from the end of the string.
' ** This should be set to -1 to search the entire string from the end to beginning.
Print InStrRev( s, "professional", -1, vbTextCompare )   ' Prints 34
Print InStrRev( s, "quicktest", -1, vbTextCompare )      ' Prints 24
Print InStrRev( s, "QUICKTEST", -1, vbTextCompare )      ' Prints 24

Posted in VBScript

Leave a Reply

You must be logged in to post a comment.

This article was viewed 556 times