VBScript InStrRev Function

Definition and Usage

The InStrRev function can return the position of the first occurrence of a string in another string. The search starts from the end of the string, but the returned position is counted from the beginning of the string.

The InStrRev function can return the following values:

  • If string1 is "" (zero length) - InStr returns 0
  • If string1 is Null - InStr returns Null
  • If string2 is "" - InStr returns start
  • If string2 is Null - InStr returns Null
  • If string2 is not found - InStr returns 0
  • If string2 is found in string1, InStr returns the position of the matching string.
  • If start > Len(string1) - InStr returns 0

Tip:Please refer to InStr 函数.

Syntax

InStrRev(string1,string2[,start[,compare]])
Parameters Description
start Optional. Specifies the starting position for each search. The default is the first character of the search. If the compare parameter is specified, this parameter must also be specified.
string1 Required. The string to be searched.
string2 Required. The string to search for.
compare

Required. Specifies the type of string comparison to use. The default is 0. The following values can be used:

  • 0 = vbBinaryCompare - Execute binary comparison.
  • 1 = vbTextCompare - Execute text comparison.

Instances

Example 1

dim txt,pos
txt="This is a beautiful day!"
pos=InStrRev(txt,"his")
document.write(pos)

Output:

2

Example 2

dim txt,pos
txt="This is a beautiful day!"
'textual comparison'
pos=InStrRev(txt,"B",-1,1)
document.write(pos)

Output:

11

Example 3

dim txt,pos
txt="This is a beautiful day!"
'binary comparison'
pos=InStrRev(txt,"T")
document.write(pos)

Output:

1

Example 4

dim txt,pos
txt="This is a beautiful day!"
'binary comparison'
pos=InStrRev(txt,"t")
document.write(pos)

Output:

15