Skip to content

RightPos

See Also: String Functions, Pos, Left, Length, Mid, Right

Purpose

Returns the ordinal position in code points of the last occurrence from the left of a substring in a host string or 0 (zero) if the substring is not found.

Return Type

Integer

Syntax

(RightPos( {sub-string}, {host-string} [, {StartingPosition}] [, {length} ] ))

Parameters

  • {sub-string}: The string value that RightPos searches for in {host-string}.

  • {host-string}: The string value RightPos searches for an occurrence of {sub-string} in.

  • {StartingPosition} (optional): Specifies the starting (left-most) position in {host-string} where the search is to begin. The position is in code points, 1-based, where the first position in {host-string} is 1.
    If less than 1, the function fails and returns 0. If greater than the last valid position in the range, it is adjusted to the last valid position.

  • {length} (optional): Specifies the maximum number of code points that should be searched in {host-string}.
    If less than 1, the function always fails and returns 0. If greater than the valid length within the range, it is adjusted to the longest valid length in the range. If not specified, the longest valid length in the range is used.

What It Does

RightPos finds the last (right-most) occurrence of {sub-string} in {host-string} (so the search starts at the left-most position moving right through {host-string}) and returns the position of the first character of {sub-string} as it stands in {host-string}.

Sample

This sample shows how to search a string for a substring using different search lengths and starting positions.

Assuming:

String sTest
Integer iPos
Move "12341234" to sTest

The following code returns position 7, the right-most occurrence of '3':

Move (RightPos("3", sTest)) to iPos

The following code returns position 3, the right-most occurrence of '3' between positions 1 and 6. This means that the portion of the host string in blue is searched: "12341234".

Move (RightPos("3", sTest, 1, 6)) to iPos

The following code returns position 0, since '3' does not occur in the substring of sTest between positions 4 (starting position) and 5 (starting position + length 2). This means that the portion of the host string in blue is searched: "12341234".

Move (RightPos("3", sTest, 4, 2)) to iPos

The following code returns position 7, the right-most occurrence of '3' following position 4. This means that the portion of the host string in blue is searched: "12341234".

Move (RightPos("3", sTest, 4)) to iPos

Notes

  • If {sub-string} or {host-string} are of type other than string, their value will be converted to a string for output.