Tokenize a string: Difference between revisions

Line 2,348:
END ' | Program end.
</lang>
 
'''Alternative method using word$ function:'''
<lang vb>a$ = "Hello,How,Are,You,Today" ' | Initialize original string.
DIM t$(LEN(a$) / 2) ' | Create an overestimated sized array.
FOR nd = 1 TO LEN(a$) ' | Start loop to find each comma.
IF MID$(a$, nd, 1) = "," THEN ' | If a comma is found...
tc = tc + 1 ' | Increment tc for each found comma.
t$(tc) = word$(a$, tc, ",") ' | Assign tc word to t$(tc) array.
END IF ' | End decision tree.
NEXT ' | End loop.
t$(tc + 1) = word$(a$, tc + 1, ",") ' | Assign last word to next array position.
ft$ = t$(1) ' | Start final return string ft$ with first array value.
FOR ne = 2 TO tc + 1 ' | Start loop to add periods and array values.
ft$ = ft$ + "." + t$(ne) ' | Concatenate a period with subsequent array values.
NEXT ' | End loop.
PRINT ft$ ' | Print final return string ft$.
 
FUNCTION word$ (inSTG$, inDEC, inPRM$) ' | word$ function accepts original string, word number, and separator.
inSTG$ = inSTG$ + inPRM$ ' | Add a separator to the end of the original string.
FOR na = 1 TO LEN(inSTG$) ' | Start loop to count total number of separators.
IF MID$(inSTG$, na, 1) = inPRM$ THEN nc = nc + 1 ' | If separator found, increment nc.
NEXT ' | End loop.
IF inDEC > nc THEN word$ = "": GOTO DONE ' | If requested word number (inDEC) is greater than total words (nc), bail.
FOR nd = 1 TO inDEC ' | Start loop to find requested numbered word.
last = st ' | Remember the position of the last separator.
st = INSTR(last + 1, inSTG$, inPRM$) ' | Find the next separator.
NEXT ' | End loop.
word$ = MID$(inSTG$, last + 1, st - last - 1) ' | Return requested word.
DONE: ' | Label for bail destination of word count error check.
END FUNCTION ' | End of function.</lang>
 
=={{header|R}}==
306

edits