PDA

View Full Version : Usage of the INSTR Keyword



Michael Clease
28-05-2007, 01:23
' Usage of the INSTR Keyword example
'
'
' Written by Abraxas

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 Startpos as dword value 0
DIM n as DWORD value 0

n = INSTR(StartPos, MainString, MatchString)

sMSG += "The number of times the word" & $CRLF & $CRLF
sMsg += MatchString & $CRLF & $CRLF
sMsg += "Appears = " & n & $CRLF & $CRLF

MsgBox 0, sMsg

ErosOlmi
28-05-2007, 07:14
Abraxas,

in this case it is not the number of times the word "nice" appears in the buffer but the position of the first occurrence.
The following variation is instead giving the number of times:


' Usage of the INSTR Keyword example
'
'
' Written by Abraxas

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


Maybe I will add both.

Thanks
Eros

kryton9
28-05-2007, 08:49
having both would be handy.

Michael Clease
28-05-2007, 09:59
Sorry about that it was late and my brain was thinking of my pillow.

ErosOlmi
28-05-2007, 10:20
Abraxas, no need to excuse.
You are making a fantastic job I cannot pay but I would.
Just to let you know I really much appreciate your effort. You will see your work in next help release.

Thanks again.
Eros