POS() Function - Scan a String

Syntax

POS(stringA relation stringB {{,intA},intB})

Description

The POS() function scans stringB for a substring with a specific relationship to stringA.

Parameter Description
stringA String sought.
relation

Relational operator, which can be one of the following:

= Equal to

<> Not equal to

< Less than

<= Less than or equal to

>Greater than

>= Greater than or equal to

stringB String to be scanned.
intA

Search interval.

For example, if intA is a 3, then POS() looks for substrings beginning at every third character in stringB. A backward scan may be performed by giving a negative value for intA. For example, if a -3 is given, POS() starts with the third character from the end of stringB and scans for substrings beginning at every third character, skipping backward. A value of 0 for intA is not allowed. If no value is given, intA defaults to 1.

intB

Occurrence of the desired substring.

For example, a value of 3 would cause POS() to find the third occurrence of any substring(s) satisfying the relation. If intB is not given, the beginning character position of the first occurrence will be reported. If intB is zero, the total number of occurrences is returned, instead of the position of a specific occurrence.

If the relation condition is satisfied, POS() returns the position of the first character in stringB of the matched substring. If the search fails, POS() returns zero.

POS() follows these logic steps:

  1. If stringA or stringB is null, return 0.

  2. Start with the first byte in stringB if intA is positive, or the Nth byte from the end of stringB if intA is negative (-N).

  3. If past either the beginning or end of stringB, return 0 (or occurrence count if intB is 0).

  4. Compare stringA with the substring at the current position in stringB. The length of the substring will be either the length of stringA or the remainder of stringB, whichever is shorter.

  5. If the given relation is true, and if this was the Nth successful try (specified by intB=N), then return the current scan position.

  6. If the relation was not satisfied, then bump the scan position (backwards if intA is negative), go to step 3, and try again.

Examples

>A$="MONTUEWEDTHUFRISATSUN"
>PRINT POS("WED"=A$)
7
>PRINT POS("ON"=A$)
2
>PRINT POS("ON"=A$,3)
0
>PRINT POS("SAT"<A$)
4
1000 REM - remove trailing spaces from A$
1010 LET A$=A$(1,POS(" "<>A$,-1))

See Also

Functions - Alphabetical Listing