INSTR
<< Click to Display Table of Contents >> Navigation: ThinBASIC Core Language > BuiltIn Functions > String functions > INSTR |
Description
Search a string for the first occurrence of a specified character or string.
Syntax
n = INSTR([StartPos,] MainString, [ANY] MatchString [, nthOccurience ])
Returns
Number
Parameters
Name |
Type |
Optional |
Meaning |
StartPosition |
Number |
Yes |
Search starting point inside MainString |
MainString |
String |
No |
String to search into |
MatchString |
String |
No |
String to search for |
nthOccurience |
Number |
Yes |
Search the nth position of MatchString |
Remarks
If StartPosition is omitted, search will start from first byte of MainString going left to right.
If StartPosition is positive, search will go from left to right.
If StartPosition is negative, search will go from right to left.
If the ANY keyword is specified, MatchString specifies a list of single characters to be searched for individually.
Restrictions
INSTR is case-sensitive, meaning that upper-case and lower-case letters must match exactly in MatchString and MainString.
If MatchString is null, INSTR returns 1 (if StartPos not specified) or StartPos (if StartPos is specified).
See also
String Handling, EXTRACT$, LCASE$, LEFT$, LTRIM$, MID$, RIGHT$, RTRIM$, TALLY, TRIM$, UCASE$, VERIFY
Examples
Thanks to Abraxas for the following script example
' Usage of the INSTR Keyword example
Dim MainString As String VALUE "My nice long string with some nice words that repeat themselves which is nice"
Dim MatchString As String VALUE "nice"
Dim sMsg As String
Dim Startpos As DWORD
Dim Pos As DWORD
Pos = INSTR(StartPos, MainString, MatchString)
sMSG += "The word" & $CRLF & $CRLF
sMsg += MatchString & $CRLF & $CRLF
sMsg += "Appears for the first time at position = " & Pos & $CRLF & $CRLF
MSGBOX 0, sMsg
'--------------------------------------------------------
'---Thanks to Abraxas for this example
'--------------------------------------------------------
' Usage of the INSTR Keyword example
Dim MainString As String VALUE "My nice long string with some nice words that repeat themselves which is nice"
Dim MatchString As String VALUE "nice"
Dim sMsg As String VALUE ""
Dim Pos As Long VALUE -1
Dim nTimes As DWORD
While Pos <> 0
Pos = INSTR(Pos + 1, MainString, MatchString)
If Pos > 0 Then
INCR nTimes
End If
Wend
sMSG += "The number of times the word" & $CRLF & $CRLF
sMsg += MatchString & $CRLF & $CRLF
sMsg += "Appears = " & nTimes & $CRLF & $CRLF
MSGBOX 0, sMsg