LastPos PowerScript function
Description
Finds the last position
of a target string in a source string.
Syntax
|
1 |
<span>LastPos</span> ( <span>string1</span>, <span>string2</span> {, <span>searchlength</span> } ) |
|
Argument |
Description |
|---|---|
|
string1 |
The string in which you want to find string2. |
|
string2 |
The string you want to find in string1. |
|
searchlength |
A long that limits the search to the |
Return Values
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
This statement returns 6, because the position of
the last occurrence of RU is position 6:
|
1 |
<span>LastPos</span>("BABE RUTH", "RU") |
This statement returns 3:
|
1 |
<span>LastPos</span>("BABE RUTH", "B") |
This statement returns 0, because the case does not
match:
|
1 |
<span>LastPos</span>("BABE RUTH", "be") |
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:
|
1 |
<span>LastPos</span>("BABE RUTH", "RU", 4) |
These statements change the text in the SingleLineEdit sle_group.
The last instance of the text NY is changed to North East:
|
1 |
long place_nbr |
|
1 |
place_nbr = <span>LastPos</span>(sle_group.Text, "NY") |
|
1 |
sle_group.SelectText(place_nbr, 2 ) |
|
1 |
sle_group.ReplaceText("North East") |
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:
|
1 |
string s, ls_left, ls_right |
|
1 |
integer li_tab |
|
1 |
|
1 |
s = dw_groups.GetBandAtPointer() |
|
1 |
li_tab = <span>LastPos</span>(s, "~t") |
|
1 |
|
1 |
ls_left = Left(s, li_tab - 1) |
|
1 |
ls_right = Mid(s, li_tab + 1) |
These statements tokenize a source string backwards:
|
1 |
// Tokenize the source string backwards<br>// Results in "pbsyc126.dll powerbuilder <br>// shared sybase programs c:<br> <br>string sSource = &<br>  'c:programssybasesharedpowerbuilderpbsyc126.dll'<br>string sFind = '''<br>string sToken<br>long llStart, llEnd<br> <br>llEnd = Len(sSource) + 1<br> <br>DO<br>   llStart = LastPos(sSource, sFind, llEnd)<br>   sToken = Mid(sSource, (llStart + 1), &<br>      (llEnd - llStart))<br>   mle_comment.text += sToken + ' '<br>   llEnd = llStart - 1<br>LOOP WHILE llStart > 1 |