I am creating a simple chat programme in QBasic that will answer questions based on some specific key words present in the user input.therefore I need a way to search for a sub string (I.e. A specific word)within a string. So, please help me.
how to search for a sub string within a string in QBasic
2.6k views Asked by Nilaksh Singh At
3
There are 3 answers
0
On
I think INSTR is usually used as follows:
sent$ = "This is a sentence"
PRINT INSTR(1, sent$, "is")
PRINT INSTR(4, sent$, "is")
PRINT INSTR(1, sent$, "word")
the first PRINT command will print a '3' since the first location of "is" within the sentence is at position 3. (The 'is' in 'This')
the second PRINT command starts searching at position 4 (the 's' in 'This'), and so finds the "is" at position 6. So it will print '6'.
the third PRINT command will print a '0' since there is no instance of "word" in the sentence.
To find out if a string contains a certain (sub-)string, you can do this:
And no, I was not able to test this, as a no longer have QBasic on my PC ;-) According to the link from the comment above >= 1 is ok