INSTRREV: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
{{ | {{PageParameters}} | ||
* The optional literal or variable [[INTEGER]] {{Parameter|start%}} indicates where in the {{Parameter|baseString$}} the search must start, counted from the left. | * The optional literal or variable [[INTEGER]] {{Parameter|start%}} indicates where in the {{Parameter|baseString$}} the search must start, counted from the left. | ||
* The {{Parameter|baseString$}} is a literal or variable [[STRING]] value to be searched for an exact match including [[UCASE$|letter cases]]. | * The {{Parameter|baseString$}} is a literal or variable [[STRING]] value to be searched for an exact match including [[UCASE$|letter cases]]. | ||
Line 26: | Line 26: | ||
{{CodeStart}} | {{CodeStart}} | ||
fullPath$ = "C:\Documents and Settings\Administrator\Desktop\qb64\internal\c\libqb\os\win\libqb_1_2_000000000000.o" | fullPath$ = "C:\Documents and Settings\Administrator\Desktop\qb64\internal\c\libqb\os\win\libqb_1_2_000000000000.o" | ||
file$ = {{Cl|MID$}}(fullPath$, {{Cl|_INSTRREV}}(fullPath$, "\") + 1) | file$ = {{Cl|MID$ (function)|MID$}}(fullPath$, {{Cl|_INSTRREV}}(fullPath$, "\") + 1) | ||
{{Cl|PRINT}} file$ | {{Cl|PRINT}} file$ | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Line 65: | Line 65: | ||
Total spaces found: 13 | Total spaces found: 13 | ||
{{OutputEnd}} | {{OutputEnd}} | ||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[MID$]], [[INSTR]] | * [https://qb64phoenix.com/forum/showthread.php?tid=1269 Featured in our "Keyword of the Day" series] | ||
* [[MID$ (function)]], [[INSTR]] | |||
* [[SPACE$]] | * [[SPACE$]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 17:57, 25 May 2024
The _INSTRREV function searches for a substring inside another string, but unlike INSTR it searches from right to left.
Syntax
- position% = _INSTRREV([start%,] baseString$, subString$)
Parameters
- The optional literal or variable INTEGER start% indicates where in the baseString$ the search must start, counted from the left.
- The baseString$ is a literal or variable STRING value to be searched for an exact match including letter cases.
- The subString$ is a literal or variable STRING value being searched.
Description
- The function returns the position% in the baseString$ where the subString$ was found, from right to left.
- position% will be 0 if the search found no matches in the base string.
- _INSTRREV returns 0 if an empty baseString$ is passed, and returns LEN(baseString$) with an empty subString$.
- The start% position is useful when making multiple searches in the same string. See the example below.
- The subString$ should be smaller or equal in length to the baseString$, or 0 is returned.
- A start% value of 0 or less starts search from the end of the baseString$ (same as not passing a start% parameter).
Examples
Example 1: Separating a file name from a full path.
fullPath$ = "C:\Documents and Settings\Administrator\Desktop\qb64\internal\c\libqb\os\win\libqb_1_2_000000000000.o" file$ = MID$(fullPath$, _INSTRREV(fullPath$, "\") + 1) PRINT file$ |
libqb_1_2_000000000000.o |
Example 2: Searching for multiple instances of a substring inside a base string, going from the end to the start.
sentence$ = " This is a string full of spaces, including at start and end... " PRINT sentence$ DO findPrevSpace% = _INSTRREV(findPrevSpace% - 1, sentence$, SPACE$(1)) IF findPrevSpace% = 0 THEN LOCATE 4, 1 PRINT "No more spaces" EXIT DO END IF LOCATE 2, findPrevSpace% PRINT "^" totalSpaces = totalSpaces + 1 IF findPrevSpace% = 1 THEN LOCATE 4, 1 PRINT "Last space found at position 1" EXIT DO END IF LOOP PRINT "Total spaces found: "; totalSpaces |
This is a string full of spaces, including at start and end... ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ Last space found at position 1 Total spaces found: 13 |
See also