POS
Return Position of Substring
This numeric function is used to scan a reference string for the occurrence of a specified substring and return a numeric value indicating its position. It can also count the number of times the substring occurs in the reference string.
Syntax
Section titled “Syntax”POS (search-string relational-operator reference-string [, step-value [,occurrence]])search-string- is any string. It specifies what is being looked for.
relational-operator- is any valid relational operator. The valid relational operators are: <, >, =, <>, ><, =>, >=, =<, <=.
reference-string- is any string. It specifies what is being searched.
step-value- is an integer that specifies the size of the steps (the number of characters to skip after each test) that are used to search the reference string. The default is 1.
occurrence- any numeric that specifies which occurrence of the substring is desired. If not specified, this value defaults to the first occurrence. Specify 0 to return the number of occurrences.
Remarks
Section titled “Remarks”Negative step-value is generally available starting with release level 8.0.
Positive step-value indicates a left to right search and negative step-value indicates a right to left search.
The positional value returned is dependent on the relational operator specified and indicates the position of the first character in the string that satisfies the indicated relation. The leftmost position of reference-string is 1.
Relationships are established on the basis of ASCII code sequence. See the ASCII code chart in Volume I for additional information on the values assigned to characters.
If the relational condition is not found, the value returned is 0.
Starting with release level 8.3.1, this function can be used to return the number of
occurrences of a substring in the reference-string. Specify 0 for occurrence.
Examples
Section titled “Examples”For the string A$ = “XXSUBSTRXX”
POS ("S" = A$)returns the value 3.
POS ("S" = A$,1,2)returns the value 6, the position of the second “S”.
POS ("S" > A$)returns the value 5 (the position of B, the first character in A$ that satisfies the relation where “S” is greater).
POS ("Z" < A$)returns the value 0 (no substring of A$ satisfied the relation where “Z” is less).
LET X = POS ("TR" = A$, -2)assigns X the value 7 after the second test (test 1 looks at “XX” and test 2 looks at “TR”).
LET X = POS ("TR" = A$, 3)finds “TR” on the third test (“XX”, “UB”, “TR”).
LET X = POS ("TR" = A$, -3)returns a 0, not finding “TR”, since it is not on a boundary, from right to left, of 3 characters.
LET X = POS("X" = A$, 1, 0)counts the number of times the X character occurs in the “XXSUBSTRXX” string. The value 4 is assigned to the X variable.
From the Thoroughbred Basic Language Reference, Volume II: F–Q, version 8.9.1. Open this page in the PDF