Pos: Difference between revisions
No edit summary |
|||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
POS(<[[string]]>,[^]<string>,[[-]<start>]) | |||
The '''Pos''' [[internal function]] returns the position of the first character of a substring in str1$ that matches str2$. Returns 0 if there is no match or str2$ is null. | The '''Pos''' [[internal function]] returns the position of the first character of a substring in str1$ that matches str2$. Returns 0 if there is no match or str2$ is null. | ||
Line 5: | Line 5: | ||
The optional '''start''' parameter specifies the position on which to begin the comparison. When '''start''' is positive, the search moves toward the end of the string. When '''start''' is negative, the search moves backward toward the beginning of the string. | The optional '''start''' parameter specifies the position on which to begin the comparison. When '''start''' is positive, the search moves toward the end of the string. When '''start''' is negative, the search moves backward toward the beginning of the string. | ||
If str2$ begins with a caret ^, then the ^ is stripped and the search becomes case insensitive. A configuration statement will permit use of a flag character other than ^. | If str2$ begins with a caret ^, then the ^ is stripped and the search becomes case insensitive. A configuration statement will permit use of a flag character other than ^ (as of 4.2). | ||
Given the (4.2+) POS use of the "^" character, to search for '^' with | |||
"let X=POS(string$,'^',5)" you will need to either turn the search character | |||
off with CONFIG SEARCH_CHAR OFF or replace the search character with something else as in CONFIG SEARCH_CHAR 7E (where 7E is the hexadecimal representation of an alternate search character). | |||
For disambiguation purposes, see also [[Pos Parameter]]. | For disambiguation purposes, see also [[Pos Parameter]]. | ||
Line 13: | Line 15: | ||
====Comments and Examples==== | ====Comments and Examples==== | ||
The program below will print the numbers 2, | The program below will print the numbers 2, 7, 7 and 7. | ||
00100 LET A$="WXYZ WXYZ" | 00100 LET A$="WXYZ WXYZ" | ||
00200 LET B$="XY" | 00200 LET B$="XY" | ||
00300 PRINT POS(A$,B$) | 00300 PRINT POS(A$,B$) ! find 1st B$ | ||
00400 PRINT POS(A$,B$, | 00400 PRINT POS(A$,B$,3) ! find 1st B$ after position 3 | ||
00500 PRINT POS(A$,B$,POS(A$,B$)+1) ! find 2nd B$ | 00500 PRINT POS(A$,B$,POS(A$,B$)+1) ! find 2nd B$ | ||
00600 PRINT POS(A$,B$,-1) | 00600 PRINT POS(A$,B$,-1) ! find final B$ (search backwards from the end) | ||
The POS function is often used to determine if a string matches any items listed in a longer string. The crucial information is not the column position of the match, but the zero (when not found) or positive (when found) value returned. The example below illustrates reading a display file one character at a time by using EOL=NONE and dimensioning a variable to be used with LINPUT so that its maximum string length is one. Using POS in line 40, each incoming character is tested to determine which ones are vowels. | The POS function is often used to determine if a string matches any items listed in a longer string. The crucial information is not the column position of the match, but the zero (when not found) or positive (when found) value returned. The example below illustrates reading a display file one character at a time by using EOL=NONE and dimensioning a variable to be used with LINPUT so that its maximum string length is one. Using POS in line 40, each incoming character is tested to determine which ones are vowels. |
Latest revision as of 15:30, 16 December 2021
POS(<string>,[^]<string>,[[-]<start>])
The Pos internal function returns the position of the first character of a substring in str1$ that matches str2$. Returns 0 if there is no match or str2$ is null.
The optional start parameter specifies the position on which to begin the comparison. When start is positive, the search moves toward the end of the string. When start is negative, the search moves backward toward the beginning of the string.
If str2$ begins with a caret ^, then the ^ is stripped and the search becomes case insensitive. A configuration statement will permit use of a flag character other than ^ (as of 4.2).
Given the (4.2+) POS use of the "^" character, to search for '^' with "let X=POS(string$,'^',5)" you will need to either turn the search character off with CONFIG SEARCH_CHAR OFF or replace the search character with something else as in CONFIG SEARCH_CHAR 7E (where 7E is the hexadecimal representation of an alternate search character).
For disambiguation purposes, see also Pos Parameter.
Comments and Examples
The program below will print the numbers 2, 7, 7 and 7.
00100 LET A$="WXYZ WXYZ" 00200 LET B$="XY" 00300 PRINT POS(A$,B$) ! find 1st B$ 00400 PRINT POS(A$,B$,3) ! find 1st B$ after position 3 00500 PRINT POS(A$,B$,POS(A$,B$)+1) ! find 2nd B$ 00600 PRINT POS(A$,B$,-1) ! find final B$ (search backwards from the end)
The POS function is often used to determine if a string matches any items listed in a longer string. The crucial information is not the column position of the match, but the zero (when not found) or positive (when found) value returned. The example below illustrates reading a display file one character at a time by using EOL=NONE and dimensioning a variable to be used with LINPUT so that its maximum string length is one. Using POS in line 40, each incoming character is tested to determine which ones are vowels.
00010 DIM X$*1 00020 OPEN #1:"name=datafile,EOL=NONE",DISPLAY,INPUT 00030 LINPUT #1: X$ EOF DONE 00040 IF POS("AaEeIiOoUu",X$) > 0 THEN PRINT X$; " -is a vowel" 00050 GOTO 30 00060 DONE: CLOSE #1:
Related Functions
- See also Pos (parameter).