INSTR()
Jump to navigation
Jump to search
Description
- Returns position of string2 within first string, string1. Begins search at starting parameter, if supplied.
Syntax
- instr(string1, string2, [starting])
Hints
- If the second string is found more that once, only the position of the first found (leftmost) will be returned.
- But you can continue using returned result for starting parameter, see second example.
- INSTR returns 0 if the string is not found (starting from the beginning or supplied starting parameter).
- There are three main uses of INSTR function:
- checking if string2 exists in string1 (first example)
- using returned position for operations on string1 (Replace$() function)
- using returned position for it's numerical value (hex2Dec() function)
Example
' User chooses...
[start]
input "Chose 1 or 2 "; choice
choice$ = str$(choice)
if instr(choice$, "1") > 0 then
print "You chose 1."
print "You are correct!"
else
print "Sorry. Wrong choice."
end if
print
input "Do you want to play again, yes or no? "; yesno$
if instr("yes", lower$(yesno$)) > 0 then
print "You answered yes."
print "Returning to start now."
goto [start]
else
print "OK. You don't want to continue. Bye."
end if
end
'Example for using Instr for finding all instances of a word in a text
duchessQuote$ = "Be what you would seem to be -- or, if you'd like it put more simply -- " +chr$(13) _
+"Never imagine yourself not to be otherwise than what it might appear to others " +chr$(13) _
+"that what you were or might have been was not otherwise " +chr$(13) _
+"than what you had been would have appeared to them to be otherwise." +chr$(13) _
print duchessQuote$
print "finding all 'you' in text:"
i=0
do
i = instr(duchessQuote$, "you", i+1)
if i<>0 then print "Found at position ";i
loop until i=0
print "That's all"
Useful Procedures
'finds all occurences of old$ in string$ (using instr()) and replaces them with new$
FUNCTION Replace$( string$, old$, new$)
pos = 1
WHILE pos
prevpos = pos
pos = instr(string$, old$, pos)
IF pos THEN
Replace$ = Replace$ + mid$(string$, prevpos, pos - prevpos) + new$
pos = pos + len(old$)
END IF
WEND
Replace$ = Replace$ + Mid$( string$,prevpos)
END FUNCTION
'Converts HEX string to (decimal) number. So, "FF"->255
function hex2Dec(s$)
hex2Dec = 0
For I = 1 To Len(s$)
c$ = upper$(Mid$(s$, I, 1))
If InStr("0123456789ABCDEF", c$) = 0 Then exit function 'like VAL, it stops on first non-valid symbol
hex2Dec = hex2Dec * 16 + InStr("0123456789ABCDEF", c$) - 1
Next I
End Function
' Converts hexadecimal number (string) to decimal number.
' Processes the hex number from right to left, so any hex notation is allowed.
' Examples: &hFF = 0xFF = FF
FUNCTION JBhexdec(HexString$)
ValidHex$ = "0123456789ABCDEF"
power = 0
JBhexdec = 0
for i = len(HexString$) to 1 step -1
HexDigit$ = upper$(mid$(HexString$, i, 1))
' exit at first non-hexadecimal digit
if instr(ValidHex$, HexDigit$) = 0 then exit for
JBhexdec = JBhexdec + (instr(ValidHex$, HexDigit$) - 1) * 16 ^ power
power = power + 1
next
END FUNCTION