Introduction | Language Structure | IB Statements | File System | Comet 32 Runtime | Index |
| Syntax: | POS(string-1, string-2) |
| Discussion: |
The POS function returns the position of one specified string
argument within a second string argument.
The first string argument (string-1) is a string value whose position within the second string argument is to be determined. The second string argument (string-2) is the string value to be searched for the first string argument. Both string arguments can be string constants, single-element string variables, or string array elements. Either the first or the second string argument (but not both) can be a string expression or a string function. The value returned by the POS function is the position within string-2 of the first character of the first occurrence (from left to right) of a substring equal to string-1. For internal string values, counting begins at character position 1. If string-1 does not equal a substring of string-2, the POS function returns a value of zero. The intermediate result of the POS function is stored in an accumulator with a precision of 16.8 If this result is then stored in a receiving numeric variable, its precision is adjusted to the defined precision of that variable. |
| Example 1: |
X = POS(A$,B$)In the above example, the value contained in the string variable A$ is compared to the value stored in B$. If a match is found, the POS function returns the matching position; this value is then stored in the numeric variable X. For example, if A$ equals "AT" and B$ equals "SIGNATURE", then the POS function will return a value of 5 (the starting position of the string "AT" within the value "SIGNATURE".) |
| Example 2: |
VALUE = POS(STR$,"ABCDEF")In this example, the value of string variable STR$ is compared to the string constant "ABCDEF". If the value of STR$ matches a portion of the constant, its position is returned and stored in the numeric variable VALUE. For example, if STR$ equals "B", the function will return a value of 2 (since "B" is in the second position of the string constant). Or, if STR$ equals "Z", the function will return a value of zero, since "Z" is not contained in the constant. Also, if STR$ equals "DE", the function will return a value of 4 (the substring "DE" starts at the fourth position of the constant). |
| Example 3: |
N = POS("SMITH",NAME$)
In this example, the string constant "SMITH" is compared to the
string variable NAME$. If "SMITH" is located anywhere within
NAME$, its position will be returned and stored in the numeric
variable N.
Note: If "SMITH" is not found in the variable NAME$, the POS function will return a value of zero. This programming technique can be useful for determining if a particular string value is contained within a specific data field. |