A function to replace n-th word in a string with another string. Looks almost the same as the word$() function, but returns a modified string, rather than the word.

Parameters:
replaceWord$(original_string$, n, delimiter$, replace_with$)

Let delimiter$ = " " to have a space as the word delimiter.

STRING$ = "The quick brown fox jumps over the lazy dog."
PRINT "Function replaceWord:"
PRINT TAB(5); "Replaces n-th word in a string with a"
PRINT TAB(5); "string, using a user specified delimiter."
PRINT
PRINT "Example:"
PRINT TAB(5); "String: "; STRING$
PRINT
PRINT TAB(5); "Replacing 2nd word..."
PRINT TAB(5); replaceWord$(STRING$, 2, " ", "slow")
PRINT
PRINT TAB(5); "Deleting 3rd word..."
PRINT TAB(5); replaceWord$(STRING$, 3, " ", "")
END
 
'*********************************************************************************************
FUNCTION replaceWord$(STRING$, wordNo, delimiter$, replaceWith$)
    i = 1
    WHILE word$(STRING$, i, delimiter$) <> ""
        IF i = wordNo THEN
            IF replaceWith$ <> "" THEN _
               replaceWord$ = replaceWord$; delimiter$; replaceWith$
            ELSE
            replaceWord$ = replaceWord$; delimiter$; word$(STRING$,i,delimiter$)
        END IF
        i = i+1
    WEND
    replaceWord$ = MID$(replaceWord$,2)
END FUNCTION