LastPos

Description

Finds the last position of a target string in a source string.

Syntax

LastPos ( string1, string2 {, searchlength } )

Argument

Description

string1

The string in which you want to find string2.

string2

The string you want to find in string1.

searchlength (optional)

A long that limits the search to the leftmost searchlength characters of the source string string1. The default is the entire string.

Returns

Long. Returns a long whose value is the starting position of the last occurrence of string2 in string1 within the characters specified in searchlength. If string2 is not found in string1 or if searchlength is 0, LastPos returns 0. If any argument’s value is null, LastPos returns null.

Usage

The LastPos function is case sensitive. The entire target string must be found in the source string.

Examples

Example 1

This statement returns 6, because the position of the last occurrence of RU is position 6:

LastPos("BABE RUTH", "RU")

Example 2

This statement returns 3:

LastPos("BABE RUTH", "B")

Example 3

This statement returns 0, because the case does not match:

LastPos("BABE RUTH", "be") 

Example 4

This statement searches the leftmost 4 characters and returns 0, because the only occurrence of RU is after position 4. The search length must be at least 7 (to include the complete string RU) before the statement returns 6 for the starting position of the last occurence of RU:

LastPos("BABE RUTH", "RU", 4)

Example 5

These statements change the text in the SingleLineEdit sle_group. The last instance of the text NY is changed to North East:

long place_nbr

place_nbr = LastPos(sle_group.Text, "NY")

sle_group.SelectText(place_nbr, 2 )

sle_group.ReplaceText("North East")

Example 6

These statements separate the return value of GetBandAtPointer into the band name and row number. The LastPos function finds the position of the (last) tab in the string and the Left and Mid functions extract the information to the left and right of the tab:

string s, ls_left, ls_right

integer li_tab


s = dw_groups.GetBandAtPointer()

li_tab = LastPos(s, "~t")


ls_left = Left(s, li_tab - 1)

ls_right = Mid(s, li_tab + 1)

Example 7

These statements tokenize a source string backwards:

// Tokenize the source string backwards
// Results in "pbsyc115.dll  powerbuilder  
// shared  sybase  programs  c:

string  sSource = &
  'c:\programs\sybase\shared\powerbuilder\pbsyc115.dll'
string  sFind   = '\'
string  sToken
long  llStart, llEnd

llEnd = Len(sSource) + 1

DO
   llStart = LastPos(sSource, sFind, llEnd)
   sToken = Mid(sSource, (llStart + 1), &
      (llEnd - llStart))
   mle_comment.text += sToken + '  '
   llEnd = llStart - 1
LOOP WHILE llStart > 1

See also

Pos