Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing using Mid( )

Status
Not open for further replies.

pintto

Programmer
Mar 23, 2001
2
0
0
US
Need help; how can I parse a sentence put in by a user,
using, String functions like Mid( ), Len( ), etc.
Ex: When will the new train be coming?
When
will
the
new
train
be
coming?
If you can help please do this thing is driving me nuts.
 
try this... just a quick outline

String$="When will the new train be coming"
DIM Words(1 to 100) as String

DO

i=i+1
f$=MID$(String$,i,1)
if f$ <> &quot; &quot; then Word$=Word$+f$ else GOSUB Seperate_Word

LOOP UNTIL i = LEN(String$)


Seperate_Word:

Index=Index+1
Words(Index)=Word$
Word$=&quot;&quot;
Return


 
[tt]MID$()[/tt] is fairly easy to use. You tell it where you want it to start in the string and how many characters you want it to grab. The more difficult part is determining where you want to start and how many characters you want :)

A good approach for this is the [tt]INSTR()[/tt] function. It takes either 2 or 3 parameters. Unusually, it is the first parameter which is optional :) In its two-parameter form, INSTR takes a string and a substring (in that order), and returns the offset into the string of the first occurrance of the substring, or 0 if it does not occur. In its second form, a third parameter is added in front of the other two, which is where in the string to start looking. The following code outputs the offset of each space in the input string:
[tt]
LINE INPUT &quot;Enter a string: &quot;, a$
x% = INSTR(a$, &quot; &quot;)
IF x% = 0 THEN
PRINT
&quot;There are no spaces in that string.&quot;
ELSE
PRINT
&quot;Spaces are at locations:&quot;
DO
PRINT
x%;
x% = INSTR(x% + 1, a$, &quot; &quot;)
LOOP WHILE x% <> 0
END IF
[/tt]

I'm sure you can adapt this to fit your needs. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top